Reputation: 21
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" />
Upvotes: 1
Views: 31630
Reputation: 1631
<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>
Upvotes: 0
Reputation: 1
$('#myform').on('submit', function(){
var value = $('#yourphone').val()
var regex = new RegExp(/^\+?[0-9(),.-]+$/);
if(value.match(regex)) {return true;}
return false;
});
Upvotes: 0
Reputation: 28837
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;
});
Upvotes: 7