1

can anyone help me for simple jquery numeric validation?

<input type="text" name="yourphone" id="yourphone" required style="border-radius:6px; border:1px solid #ccc; width:300px; height:25px;" />
<input type="submit" value="Send Inquiry" class="button" id="mySubmitButton" />
3
  • Numeric validation for what? Checking integers? Floats? Max/Min? I assume phone number from reading the code.. Tell us what you're trying do to, and what you've already tried to achieve it. Commented Aug 7, 2013 at 10:56
  • 3
    Sure, change the input type from text to number ! Commented Aug 7, 2013 at 10:56
  • To the right, to the right - all your answers in the list to the right Commented Aug 7, 2013 at 10:57

3 Answers 3

7

You can change your input type to number like <input type="number"... (although not all browsers support HTML5 input types).

Or you can use this:

$('#myform').on('submit', function(){
    var value = $('#yourphone').val()
    return $.isNumeric(value);
});

but phone numbers can be complex, not just numbers.

In case the user uses + ( ) - . , you can use this:
(demo)

$('#myform').on('submit', function(){
    var value = $('#yourphone').val()
    var regex = new RegExp(/^\+?[0-9(),.-]+$/);
    if(value.match(regex)) {return true;}
    return false;
});
Sign up to request clarification or add additional context in comments.

2 Comments

or just return $.isNumeric(value);
Yes, did that and added a regex alternative too.
0
$('#myform').on('submit', function(){

    var value = $('#yourphone').val()
    var regex = new RegExp(/^\+?[0-9(),.-]+$/);
    if(value.match(regex)) {return true;}
    return false;
});

Comments

0
<script type="text/javascript">
    var specialKeys = new Array();
    specialKeys.push(8); //Backspace
    $(function () {
        $(".numeric").bind("keypress", function (e) {
            var keyCode = e.which ? e.which : e.keyCode
            var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
            $(".error").css("display", ret ? "none" : "inline");
            return ret;
        });
        $(".numeric").bind("paste", function (e) {
            return false;
        });
        $(".numeric").bind("drop", function (e) {
            return false;
        });
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.