开发者

Validating Human Height using javascript

I require have a textbox to enter a human height. I require to do this using jquery. Is there a plugin or code sample that I can us开发者_JAVA百科e to, to perform the validation. Can someone please help me to validate the textbox using jquery.


You're going to want to use regex:

var height = '4\'3"'; //$('selector-to-input').val()
if (/^[0-9]+\' ?[0-9]+\"$/.match(height)) {
    //do something
}

That's not the greatest regex but it will check anything like 9' 5" or 5'7" or 5'11".

For more regex stuff check out: Regex Info

Edit: Better regex may be something like

/^[0-9]+ ?(\'|ft|cm|meters|feet|in|inches|\")?( *[1-9]+ ?(\"|inches|in|cm)?)?$/

You'll have to test it, on lunch break and need to get back to work.


Do two text boxes: one for feet, one for inches. Then put them both into the same units.

like:

var person_height = $(feet).val()*12 + $(inches).val();

This is so they only input numbers -- no characters.

Or, if you're in Europe or wherever, just ONE text box for meters. (1.5 meters)

Oh, metric system. Soon.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜