JavaScript Regex French Phone Number - javascript

I try to define a regex in Javascript who can accept separators like spaces, points, double-points and dashes. My regex is working when there is no separators but when I add space or other separator, it's not working.
I have to precise: this regex is for French phone number (0123456789 or 01.23.45.56.78 or 01 23 45 67 89,...). Also, this regex can accept "+33" who replaces the first "0".
This is my regex
var regex = /^(0|\+33)[1-9]([-.: ]?[0-9]{2}){4}$/;
Can someone tell me what is wrong?

Thank you everyone for yours quick answers!
So as Wiktor Stribiżew said, my regex was good! (Thank you for the link, I didn't know this website).
I have tried a lot of things so sorry for the late reply, but I have found where was my error!
My function is the next one (called by a onblur):
function verifTel(champ) {
var regex = /^(0|\+33)[1-9]([-.: ]?[0-9]{2}){4}$/;
if(!regex.test(champ.value)) {
surligne(champ, true);
return false;
} else {
surligne(champ, false);
return true;
}
}
surligne() is my function for changing the color of the input text. When I wrote 0123456789 my text was green but when I wrote 01.23.45.67.89 my text was red.
BUT my error wasn't in JavaScript... My error was here, in my HTML:
<input id="tel" type="number" placeholder="Téléphone" required onblur="verifTel(this)">
The type="number" accept only numbers and "e" letter. So, I have changed the type to "text" and now it's working!
Thank you everyone for your answers! Have a nice day!

Just add * after [-.: ]. With this change you'll support multiple spaces between the parts.
var regex = ^(0|\+33)[1-9]([-.: ]*?[0-9]{2}){4}$;

Related

Regular expression allowing only characters that will not break a link

Anybody can help me with regular expression which will only accept alphabetical letters from English alphabet and numbers without whitespaces ( ÖÜÕÖ and similar characters + whitespaces will break the HTML link this thing is creating )?
I currently have :
/[A-Za-z ]\S+$/
but this will allow whitespaces and ÖÜÄ and similar at the beggining.
function validatenumber(el) {
var regex = /[A-Za-z ]\S+$/;
if( !regex.test(el.value) ) {
alert('invalid value');
}else{
alert('correct value');
}
}
http://jsfiddle.net/qd7BL/1375/
Here's a fiddle.
Try
/^\w+$/
It'll allow only a non empty string, containing english alphabet letter, both cases, underscore and digits in a string.
/^[A-Za-z0-9]+$/
Solved the problem, although I should probably think is more through as much more elements are allowed in link.
Also, the best solution was to use Slugify, a plugin for jQuery which will make the input correct format.

Restrict accepting email that has non English characters

First of all, I checked possible solutions but couldn't find something that helps to solve my problem.
In short, I have existing logic that gets email from user and tests for few conditions, like make sure it doesn't have apostrophe, double # sign and consecutive dots and etc. The logic is not implemented using regex.
Now, I have new requirement, we need to restrict user from entering non English characters, by restrict I mean try to catch it while user is entering non english character or catch it while value is passed to javascript function that is verifying other conditions above.
So I found some answers here and tried them: here
Here is my code:
<input type="text" id="ctEmailAddress" name="ctEmailAddress" autocomplete="off"
size="40" maxlength="255" value="${userinfo.emailAddress}" oncopy="return false;" onpaste="return false;" onkeypress="suppressNonEng(event)">
And script:
function suppressNonEng(event){
var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
var key = String.fromCharCode(event.which);
if (englishAlphabetAndWhiteSpace.test(key)) {
return true;
}
alert ("this is not in English");
return false;
}
So I used js function from link, it is separate from current logic. The problem is, it still allows to pass non english characters, for example french.
The same applies to this function:
function suppressNonEng(event){
var key = event.which;
if(key > 128){
alert("Email address can be entered only in English. Please try again.");
}
}
Again, French letters have no problem going through it.
My question is, how I can make sure that value is english characters only? Should I use regex for that or there is better solution? Please advice. Thanks.
Description
[^\x00-\x7F]+
This regular expression will match any character that is outside ascii 0-127
Example
Live Demo
https://regex101.com/r/qN7eP6/1
Sample text
Here are some sample non-english characters: ü, ö, ß, and ñ.
Sample Matches
[0][0] = ü
[1][0] = ö
[2][0] = ß
[3][0] = ñ
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
[^\x00-\x7F]+ any character except: ascii 0-127 also known as
'\x00' to '\x7F' (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------

Finding character types in a particular word ( regex ) in javascript

How do I find lower case / special characters in a word
for example :
HELLO (this is good)
HeLL0 ( not good)
H$L&0 (not good) .
I have two regex statements that I am evaluating this value by but does not seem to help
var pattern = /[A-Z]*[A-Z,]+/;
var pattern2= /[^a-z0-9!##$%^&*()+-_?<>~`\|{}]+/;
Maybe You need regex like this:
/[^A-Z]/
This should pick up every character, which is not a capital letter. Hope it helps. Cheers.
Use this regex to mach only when the entire string is capital letters:
/^[A-Z]+$/
I think you only need to use this simple code to make that test.
var txt="Hello"; //Your text
var pattern = /^[A-Z]+$/; //Pattern for only Mayus
if ((txt.match(pattern)) && (txt!='')) // If for comprove if is only mayus and isn't empty.
{
//Correct
}
else
{
//No correct
}
I am looking for only capital letter words
Try following regex
/(?:^|\s)([A-Z]+)(?:\s|$)/g
regex101 demo

mobile number validation using regex javascript

I want mobile number in format
+91(or any other country code)-9999999999(10 digit mobile number).
I have tried /^\+[0-9]{2,3}+[0-9]\d{10}, but its not working please help
Thanks in advance
Solution in short:
// should return an array with a single match representing the phone number in arr[0]
var arr = '+49-1234567890'.match(/^\+\d{1,3}-\d{9,10}$/);
// should return null
var nullVal = 'invalid entry'.match(/^\+\d{1,3}-\d{9,10}$/);
Longer explanation:
/ start regex
^ try to match from the beginning
\+ match a + sign
\d{1,3} match a digit 1 to 3 times
- match a dash
\d{9,10} match 9 or 10 digits
$ force the matching to be only valid if can be applied until string termination
/ finish regex
Knowing what the regex does, might let you modify it to your own needs
Sometimes it is good to ignore any whitespaces you come across. \s* matches 0 or n whitespaces. So in order to be more permissive you could let users input something like ' + 49 - 1232345 '
The regex to match this would be /^\s*\+\s*\d{1,3}\s*-\s*\d{9, 10}\s*$/ (just filled the possible space locations with \s*)
Other than that: I warmly recommend mastering regexes, because they come really handy in many situations.
If you are expecting a dash in the number (which your format shows), there is nothing in your regex to match it: is the second plus in the regex meant to be a dash?
^\+[0-9]{2,3}-[0-9]\d{10}
Also note that:
some country codes are single digit (eg. 1 for North America, 7 for Russia), these will not be matched
I doubt the local part of all mobiles everywhere is always 10 digits (eg. it won't be enough in countries with large populations as mobile ownership grows)
You can simply write the following:
var pattern=/^(0|[+91]{3})?[7-9][0-9]{9}$/;
\+[0-9]{2,3}-[0-9]+
Try this. This matches a + in the beginning, two or three numbers for the country code, followed by a - followed by any number of numbers
Use the mask function
jQuery(function($){
$("#phone").mask("999-999-9999",{placeholder:" "});
});
look here http://office.microsoft.com/en-in/access-help/control-data-entry-formats-with-input-masks-HA010096452.aspx
For mobile validation please try this
<html>
<head>
<title>Mobile number validation using regex</title>
<script type="text/javascript">
function validate() {
var mobile = document.getElementById("mobile").value;
var pattern = /^[7-9][0-9]{9}$/;
if (pattern.test(mobile)) {
alert("Your mobile number : "+mobile);
return true;
}
alert("It is not valid mobile number");
return false;
}
</script>
</head>
<body>
Enter Mobile No. :
<input type="text" name="mobile" id="mobile" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

dutch Bank account validation rule

Can anybody advise me how to make a validation rule for Dutch bank accounts?
So far i could only found this on web:
regex = /[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{3}/;
This is my JavaScript:
function dutchBankAccount(input) {
var regex = /[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{3}/;
if(input.value.toString().match(regex) && !(input.value == "")) {
return true;
} else {
input.click();
input.style.border = '2px solid #F20056';
return false;
}
}
And here is my HTML code:
<li><input type="text" id="anum" placeholder="Account Number" autocomplete="off" onkeypress="return isNumberKey(event)" onBlur="isValidAnum()" onFocus="emptyAnum('anum')"/></li>
Later on when I enter a dutch bank account I get error which I'm not supposed to get. So if you know how to solve this please help me.
The regex syntax is incorrect. Try something more like this:
var regex = /[0-9]{2}\s[0-9]{2}\s[0-9]{2}\s[0-9]{3}/;
That matches strings like
32 01 28 192
Two digits followed by a space three times, then three digits. Whether that's what all Dutch bank accounts look like I don't know, though that seems like a small namespace for something like that.
(It occurs to me that /(?:\d{2}\s){3}\d{3}/ should match the same strings and it's a little shorter.)
edit — To elaborate, the regex in the original code has some problems:
The backslashe before each of the "\s" (space) characters is doubled, but it should not be. (That's assuming that Dutch bank account numbers don't actually look like "92\s31\28s\120")
Putting a single character class shortcut ("\s") in square brackets is needlessly redundant
Suffixing a regex element with "{1}" is needlessly redundant too
The real problem was the extra backslash. Also, speaking of needlessly redundant, there's no need to call ".toString()" on the value of an input element "value" attribute, and there's no need to make sure the value isn't the empty string if it has matched the pattern. In this case, an empty string cannot match the pattern, so that test is not necessary. Finally (promise), if you're just testing a regex against a string, the ".test()" method on the RegExp prototype is a little more efficient:
if (regex.test(input.value)) { // matched
// ...
}

Categories