I want to create a regular expression to check for valid dimension length x breadth x height in javascript.
F开发者_运维知识库or e.g. 90.49 x 34.93 x 40.64
Sample code I intend to use:
var dimensionRegex = 'the regular expression for validation dimension string';
var tempDimension = 'dimension string input by the user';
if (dimensionRegex.test(tempDimension)) {
return true;
} else {
return false;
}
Any help would be greatly appreciated.
Why use regexps for that? You could just split('x')
the string and then strip leading/trailing space from the array elements. Then you parseInt(elem, 10)
the array elements and you have what you need.
However, if you actually want to validate the format:
var dimValid = /^\d+(\.\d+)? x \d+(\.\d+)? x \d+(\.\d+)?$/.test(tempDimension);
This will match number x number x number
.
精彩评论