I'm trying to validate my phone address text field. Can anyone provide me a simple regex to either validate for numeric digits only or a specific 8 digit regex?
Much thanks!
$('#phone').on('input', function() {
var input = $( this );
var regex = /* regex needed here */
var is_phone = regex.test(input.val());
if (is_phone){input.removeClass("invalid").addClass("valid");}
else {input.removeClass("valid").addClass("invalid");}
});
I'm trying to validate my phone address text field. Can anyone provide me a simple regex to either validate for numeric digits only or a specific 8 digit regex?
Much thanks!
$('#phone').on('input', function() {
var input = $( this );
var regex = /* regex needed here */
var is_phone = regex.test(input.val());
if (is_phone){input.removeClass("invalid").addClass("valid");}
else {input.removeClass("valid").addClass("invalid");}
});
Share
Improve this question
edited May 9, 2016 at 11:07
georg
215k56 gold badges322 silver badges400 bronze badges
asked May 9, 2016 at 11:04
iamhxiamhx
4727 silver badges25 bronze badges
1
-
There are literally thousands of examples out there. The pattern is simple:
^\d{8}$
– Terry Commented May 9, 2016 at 11:08
1 Answer
Reset to default 3It will allow 0-9 numbers and only 8 digits. Try this:
var regex = '/^[0-9]{1,8}$/'; // if you want min 1 and max 8 numbers
var regex = '/^[0-9]{8}$/'; // if you want exact 8 numbers