开发者

javascript regular expression for dimension

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜