jqueryvalidationtextbox

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 use to, to perform the validation. Can someone please help me to validate the textbox using jquery.


Solution

  • 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.