I'm just working on some JavaScript to accept some user input via jQuery. What I'm trying to validate is 3 capital characters followed by 6 integers.
Anyone know how this can be done using a regular expression in JavaScript?
A simplified version could be /^[A-Z]{3}\d{6}$/.
A more 'compatible' version would be to use /^\p{Lu}{3}\pN{6}$/.
Simple regex could be:
/^[A-Z]{3}\d{6}$/
or, if you want to be unicode compatible:
/^\p{Lu}{3}\pN{6}$/
Easy like this:
var match = yourString.match(/^\p{Lu}{3}\pN{6}$/);
if(match) {
// tada!
} else { alert("not matched"); }
Related
I am trying to verify a pattern of 5 characters - 5 characters - 5 characters with Javascript and I want it to fail if there are more or less than 5 characters between dashes. My test is as follows:
var patterns = new RegExp("[2-46-9A-DF-HJKMP-RTVW-YX]{5}-[2-46-9A-DF-HJKMP-RTVW-YX]{5}-[2-46-9A-DF-HJKMP-RTVW-YX]{5}","gi");
if(patterns.test(fkLicense) == true) {
alert('good');
} else {
alert('bad');
}
My issue is, no matter what I set the value of fkLicense to, the test fails. Any help will be greatly appreciated.
I'm not sure what you're trying to match, but the problem is that you need to anchor your regular expression and it's alot easier to use a regular expression literal here instead ...
var patterns = /^ .... $/i
eval.in
I'm using this Wordpress plugin called 'Easy contact form' which offers standard validation methods.
It uses the following regex for phone numbers:
/^(\+{0,1}\d{1,2})*\s*(\(?\d{3}\)?\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/
Just now it allows the following formats (maybe more):
0612345678
+31612345678
But I want it to allow +316-12345678 and 06-12345678 also ... Is this possible? If so, how?
Thanks in advance!
You can use a less complex regex :
^\+?\d{2}(-?\d){8,9}$
This regex allows a + at the beginning of the phone number, then matches two digits, and after that, digits preceded (or not) by a -, for a total of 10 or 11 digits.
Now you can adapt it if the initial + is only for 11-digits phone numbers :
^\+?\d{3}(-?\d){9}|\d{2}(-?\d){8}$
My regex allow you to use - every digit. If that's an issue, it can be changed :
^\+?\d{3}(-?\d{2}){4}|\d{2}(-?\d{2}){4}$
I think this last regex will answer your needs, and it's quite simple !
When you figure out what patterns you actually want to allow and not allow. Then fill them out in this function and if the statement returns true you have your regex.
var regex = /^\+?\d{3}(-?\d{2}){4}|\d{2}(-?\d{2}){4}$/; //this is your regex, based on #Theox third answer
//allowed patterns
['0612345678', '+31612345678', '+316-12345678', '06-12345678'].every(function(test) {
return regex.exec(test) !== null;
}) &&
//disallowed patterns, none right now
[].every(function(test) {
return regex.exec(test) === null;
});
I need to check Format Phone Number.
+33xxxxxxxxx
0033xxxxxxxxx
EDIT : 0xxxxxxxxx
How can I do that with(out) regex ?
The regex to match it would be this
(0033|\+33|0)?\d{9}
I use http://regexpal.com/ for quick regex testing
if (/^(?:(?:\+|00)33|0)\d{9}$/.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
PhoneFormat.com has a javascript library that has some formatting functions that you could easily drop into your project. It will take whatever number you throw at it and try and convert it to e164 (+ 33252525252), and also format it (+33 2 52 52 52 52)
Try this regex: /^((\+|00)\d{2})?\d{9}$/
This matches each of your given cases (+YYXXXXXXXXX, 00YYXXXXXXXXX, XXXXXXXXX).
Edit: To match your edit: /^((\+|00)\d{2}|0)\d{9}$/
This is a regex that validates your examples:
/(\+|(00)|0)[0-9]{11}/.test(stringToTest)
I want to test a passwprd which must have at least 6 characters and 1 number in it. What regex string I can use with JS to get this done?
UPDATED
I forgot to write it must have at least 6 alpha characters and 1 numeric character but it should also allow special characters or any other character. Can you please modify your answers? I greatly appreciated your responses
This does smell a little like a homework question, but oh well. You can actually accomplish this concisely using a single regular expression and the "look ahead" feature.
/(?=.{6}).*\d.*/.test("helelo1")
The first bit in the brackets says "peek ahead to see if there's 6 characters". Following this we check for any number of characters, followed by a number, followed by any number of characters.
It is even possible to accomplish your goal in a single regex without having the faculty of look ahead... It's just a little hard to look at the solution and not wince:
new RegExp("[0-9].....|" +
".[0-9]....|" +
"..[0-9]...|" +
"...[0-9]..|" +
"....[0-9].|" +
".....[0-9]").test("password1")
Try this:
password.match(/(?=.*\d).{6}/);
More info here.
As far as I know this is best done with a combination of string functions and regex:
if( myPass.match(/[a-zA-Z]/).length >= 6 && myPass.match(/\d/g).length ) {
// Good passwords are good!
}
EDIT: Updated to include the new stipulations. Special characters are allowed, but not required.
if (/.{6,}/.test(password) && /\d/.test(password)) {
// success
} else {
// fail
}
/^(?=[\w\d]{6,}$)(?=.*\d)/.test(password)
requires 6 or more characters (letters, numbers or _)
requires at least one digit
won't allow any special characters
This is a js to check password,
it checks min 7 chars, contains 1 Upper case and 1 digit and 1 special character and must not contain a space, hope it will help you.
pwLength = this.value.length;
if (pwLength > 7 && pwLength < 21) {
charLengthIcon.removeClass("fail").addClass("pass");
}
else charLengthIcon.removeClass("pass").addClass("fail");
if (this.value.match(/[A-Z]/g)) {
capLetterIcon.removeClass("fail").addClass("pass");
}
else capLetterIcon.removeClass("pass").addClass("fail");
if (this.value.match(/[0-9]/g)) {
numberIcon.removeClass("fail").addClass("pass");
}
else numberIcon.removeClass("pass").addClass("fail");
if (this.value.match(/[##$%!$&~*^(){}?><.,;:"'-+=|]/g)) {
splcharIcon.removeClass("fail").addClass("pass");
}
else splcharIcon.removeClass("pass").addClass("fail");
if (this.value.match(/[\s/]/g)) {
whiteSpce.removeClass("pass").addClass("fail");
}
else whiteSpce.removeClass("fail").addClass("pass");
confirmPW();
});
I would like some help creating a regular expression for parsing a string on a textbox. I currently have these two javascript methods:
function removeIllegalCharacters(word) {
return word.replace(/[^a-zA-Z 0-9,.]/g, '');
}
$("#comment").keyup(function() {
this.value = removeIllegalCharacters(this.value);
});
I would like to replace my /[^a-zA-Z 0-9,.]/g regex for one that would accept only the following set of characters:
a-z
A-Z
0-9
áéíóúü
ÁÉÍÓÚÜ
ñÑ
;,.
()
- +
It's probably pretty simple, but I have close to none regex skills. Thanks in advance.
Just add those characters in.
function removeIllegalCharacters(word) {
return word.replace(/[^a-zA-Z 0-9,.áéíóúüÁÉÍÓÚÜñÑ();+-]/g, '');
}
return word.replace(/[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜñÑ\(\);,\.]/g, '');
You may have to use the hex escape sequence (\x##) or unicode escape sequence (\u####) for some of the non standard letters, but that will give you a good start. Or, slightly simplified:
return word.replace(/[^\w\dáéíóúüÁÉÍÓÚÜñÑ\(\);,\.]/g, '');
If I've understood your requirements correctly, you want to allow only the listed char and you want to delete rest all char. If that is the case you can simply extend your char class as:
function removeIllegalCharacters(word) {
return word.replace(/[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜñÑ;,.()]/g, '');
}
Did you try with: [^a-zA-Z 0-9;,.áéíóúüÁÉÍÓÚÜñÑ()]