Sunday, May 19, 2013

Javascript Validation isNumeric

<script type='text/javascript'>
function isNumeric(elem, helperMsg){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}
</script>
<form>
Numbers Only: <input type='text' id='numbers'/>
<input type='button'
    onclick="isNumeric(document.getElementById('numbers'), 'Numbers Only Please')"
    value='Check Field' />
</form>
...

Display:


Numbers Only:

No comments:

Post a Comment