jQuery validate phone number with with RegEx
Your regex works fine for me... you could shorten it to just /[0-9]{10}/
.
Your problem is here:
$("#call_number").val().length < 1
. If the number is 10 characters long, it will never be less than 1, no?
You probably meant something like this:
$("#call_number").val().length === 10
jQuery Phone Regex Validation
You need the following regex:
/^\+(?:\d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4})|\d{11})$/
See the regex demo
The regex you have ^\+d{1}(\d{3})\d{7}$
has d
instead of \d
(thus failing to match digits) and unescaped parentheses (thus the pattern did not match literal parentheses).
Breakdown:
^
- start of string\+
- a literal+
symbol(?:\d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4})|\d{11})
- two alternatives:\d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4})
:\d
- a digit(?:\(\d{3}\)|-\d{3})
- either(123)
like substring or-123
substring-\d{3}
- a hyphen followed with 3 digits-
- a hyphen(?:\d{2}-\d{2}|\d{4})
- 2 digits followed with a hyphen and 2 digits or 4 digits
|
- or\d{11}
- 11 digits
$
- end of string
Example of a regular expression for phone numbers
Use this rexeg
/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/
- (123) 456 7899
- (123).456.7899
- (123)-456-7899
- 123-456-7899
- 123 456 7899
- 1234567899
supported
Jquery form validation - telephone number
I would highly recommend using jQuery validate rather than re-inventing the wheel
a quick google search for uk numbers with jquery validate turned this up
filters
that looks like this
jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/);
}, 'Please specify a valid phone number'
);
Modify given regex to check if string contains no phone number
Following the logic from our chat, here is the pattern:
/^(?!.*(?:\s|^)(?:\+\d|0)(?:[()-]*\d){6,}(?!\S))/
See the regex demo.
Details:
^
- start of string(?!
- start of a negative lookahead:.*
- any zero or more chars other than line break chars, as many as possible(?:\s|^)
- whitespace or start of string(?:\+\d|0)
-+
and a digit, or a zero(?:[()-]*\d){6,}
- six or more occurrences of zero or more(
,)
or-
chars and then a digit(?!\S)
- no non-whitespace char allowed immediately to the right of the current location
)
- end of the lookahead check.
regex to validate phone number jquery
Check out this solution using negative lookahead:
^(?![8]{10}|[9]{10})[7-9]{1}[0-9]{9}$
jquery validate phone number and extension
// custom extension regex
phone: function(value, element) {
return this.optional(element) || /^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})\s*-?[2-9]\d{2}-?\d{4}(\s*x\s*\d{4})?$/.test(value);
},
Related Topics
How to Create Expand/Collapse Function for Each Row of Table - Angular6
Angular 4: How to Read Content of Text File With Httpclient
Jquery, Line Break in Textarea.. Don't Get \N and Get ↵
Angular2 Do Actions After Dom Has Finished Rendering
Allow Negative/Positive Numbers But Not Letters
How to Allow Only English Letters in Input Fields
Is There a Function in Lodash to Replace Matched Item
Javascript: Search String in Array Then Count Occurrences
Getting Name of Parent Object from Value of Object
Input Date Format With Dd/Mm/Yyyy in the Input Field
How to Remove Forward and Backward Slashes from String in JavaScript
Save Embedded Pdf from Website
Wait for Asynchronous Functions to Finish in Angular
Regex to Allow Only Certain Special Characters and Restrict Underscore
How to Convert Jquery Code to JavaScript