How would I validate with Javascript/jQuery that a phone number matches the format of +322123456?
The +32 is mandatory, and the phone number begins with it.
After the +32, 8 or 9 numbers
Use regex to do that.
/\+32\d{8,9}/
try
\+32\d{8,9}
DEMO
You use this regex : \+32[0-9]{8}[0-9]+ to check that.
Related
I'm using the Html5 pattern to validate my inputs on forms. I need to make sure an input has the following rules:
A maximum and minimum of 8 characters
The first 3 must be the specific letters wrx (lowercase)
The last 5 must be numbers.
Eg. wrx12345
Can I even do this with pattern or do I need to use JavaScript?
I believe the regex pattern you are looking for is /^wrx[0-9]{5}$/. A visual representation of this here:
And implemented in html:
<input name="example" pattern="^wrx[0-9]{5}$">
You can use regex in Javascript with this regex:
"/[wrz][\d]{5}/g".
To test the minimum = maximum length = 8, you can just test it in javascript.
If the length egual 8, use the regex
Else, show error
I think this could work
You don't need Javascript to do this.
The pattern attribute uses regular expressions so you can use something like this: ^wrx[0-9]{5}$
The ^ and $ indicates the start and end of the string. Then 'wrx' has to be matched exactly and [0-9]{5} looks for 5 number bewteen 0-9.
You can use something like RegExr to test your patterns.
I want to test if a user string is "ok so far", in that it might not be valid as a whole but it is a subset of a valid one.
I have a regex say ^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]$
such that "1234-1234-5678-5678" is valid
"1234-12" or even "1" does not match pattern but its a valid subset of a valid format, in other words the input is ok so far.
is there a neat way of doing this without making many many regexes, its friday.
Not sure if I understood well your problem, but I think you want to have something like this:
^([0-9]{4}-){1,3}[0-9]{1,4}$
Working demo
This will match set of 4 digits and can have the last set from 1 to 4 digits
You can also shorten your regex with:
^(\d{4}-){1,3}\d{1,4}$
You could possibly use one final regex for validation of the form you currently have, and a on the fly regex for the user input being valid for each subset.
My idea would be to have ([0-9]{1,4}-)+
For your case this will check as one types:
/^(\d(\d(\d(\d(-(\d(\d(\d(\d(-(\d(\d(\d(\d(-(\d)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?$/
This regex will match key for key as you type, although it is a little cumbersome.
^([0-9]{1,4}|[0-9]{4}-[0-9]{0,4}|[0-9]{4}-[0-9]{4}-[0-9]{0,4}|[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{0,4})$
Here is a live example
I am trying to generate a regex which would match the following sequence-
+91123456789,+41123456789,+21123456789.... and so on, there is no limit of phone numbers.
Basically the usage is to validate the phone numbers which users may add, phone number can be multiple and need to be separated by commas, I am already removing the empty spaces which users may add, so no worry for that.
I am not good with regex and have created the following regex but it doesn't matches the preceding phone numbers, means the whole string of phone numbers do not match-
^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9},\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$
I need to validate the user input using javascript or jquery.
Valid Phone number should be having country code like +91 or +21 etc country code can be of one or two digits, then the number of digits need to be 7 to 9.
I anyone could help, it would be highly appreciable, I have spent lot of time on this one.
To validate the whole string handling mulitple values sepparated by comma just add an group with * multiplier:
^\+\d{8,11}(,\+\d{8,11})*$
If I understand the requirements correctly, the following regex should work
\+\d{9,11}
However, you can separate the country code out, for if you need to allow for (+44)xxxxxxxxx
\+\d{2}\d{7,9}
if the requirement is to allow for 1 country code as well, adjust the regex to the following
\+\d{1,2}\d{7,10} //I think to 10, not sure on their numbers
You can update the ranges as you see fit :)
Demo: https://regex101.com/r/rJ4wM7/1
I'm using jQuery's validation plugin to validate a form.
I have a field which should only contain letters or numbers. I have tried the following regex but it's still allowing characters such as ' ; :
[a-zA-Z 0-9]$
What am I doing wrong?
Thanks in advance
Try this:
^[a-zA-Z 0-9]*$
If this is a required field, then change asterisk to plus:-
^[a-zA-Z 0-9]+$
Try this expression::
^[a-zA-Z 0-9]+$
I need to validate phone or mobile number in javascript.
It should match the following types:
+91-9198387083
9198387083
09198387083
It is a 10 digit number with one of these four prefixes '+91-', '+91', '0' or ''
Can someone suggest me a regex expression in javascript which matches all the three types.
I'd using something like this:
/^(\+91-|\+91|0)?\d{10}$/
This is very specific about one of your three allowable prefixes (or no prefix) followed by exactly 10 digits and no extra characters on the beginning of end of the string.
Test app with both valid and invalid phone numbers here: http://jsfiddle.net/jfriend00/K9bjL/
Something like this should work:
/\+?[0-9]+-?[0-9]/
10 digit number with one of these four prefixes +91-, +91, 0 or :
/(\+91-?|0)?\d{10}/
+7 (123) 45-67-890
8(123)381-198-2
and etc.
My simply mask for all of similar phones
/^[+]?(\d[^\d]*){11}$/