How can I match bangladeshi phone number using RegExp in javascript? - javascript

this following expression is working for any 11 digits phone number. But I want the first three digit to be specific operator code like (017, 016, 018, 019). And rest of the others can be any digits.
/^([\d]{3})*([\d]{8})$/

You may use an alternation here:
/^(01[6789])(\d{8})$/
Code sample:
console.log(/^(01[6789])(\d{8})$/.test('01612345678')); // pass
console.log(/^(01[6789])(\d{8})$/.test('01912345678')); // pass
console.log(/^(01[6789])(\d{8})$/.test('12312345678')); // fail

Related

HTML Form Validation - Check for Specific Characters

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.

Regex Comma Separated Phone Number

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

Trying to get a subset of data using ECMAScript Regular Expression where one string doesnt exist

So I find this really hard to explain what im doing, but its easy to show in an example.
I have these 2 strings and I need a regular expression that will only return a value for one of them
Ref Numbers: RCPT:02972, VLBL#:7158461 $930.38
2012-10- 01 5558461 abagaa
I need the 5558461 but not the 7158461, so basically a 7 digit number. I was trying to do this where Ref Numbers exists do not return anything, but if it doesn't then return the 7 digit number.
I've tried many different things but I just cannot get what I need. does anyone out there have an idea what needs to be done?
Thanks
Use negative lookahead assertion like below,
^(?!.*Ref).*\b(\d{7})\b
Use the above regex and get the number you want from group index 1.
DEMO
> var s = "Ref Numbers: RCPT:02972, VLBL#:7158461 $930.38\n2012-10- 01 5558461 abagaa"
> console.log(/^(?!.*Ref).*\b(\d{7})\b/m.exec(s)[1]);
5558461
Negative lookahead (?!.*Ref) at the first asserts that the line where the match going to occur won't have the string like Ref

Can't get number based javascript regex pattern to work

I know it should be simple, and yes i've tested on online regex sites, but i just can't get this to work.
Input string: "w_(number from 1-99),h_(number from 1-99)", e.g: "w_34,h_34"
Expected Output: number replaced, e.g "w_50,h_50"
Test:
'w_34,h_34'.replace('w_[1-9][0-9],h_[1-9][0-9]', 'w_50,h_50')
But it just returns the original string. (w_34,h_34)
You need to use a regular expression to take advantage of the regexp syntax
'w_34,h_34'.replace(/w_[1-9][0-9]?,h_[1-9][0-9]?/, 'w_50,h_50')
This will solve to only 2 digits of numbers. An alternative would be to use the * operator.
'w_34,h_34'.replace(/w_[1-9][0-9]*,h_[1-9][0-9]*/, 'w_50,h_50')
Which would allow n-length numbers to match.

Mobile or phone number validation in javascript

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}$/

Categories