Why the name validation is invalid? [duplicate] - javascript

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Amend Regular Expression to allow German umlauts, French accents and other valid European letters
(4 answers)
Closed 6 years ago.
Why the name is not valid when user is type:
André
René
François
Anne-Marie
CODE:
var myNameFilter = /^([a-zA-Z ]+)$/;
var a = $('#firstname').val();
if(a=='') {
alert("You stupid your name is empty. Fill it in NOW!!!");
return false;
} else if (!myNameFilter.test(a)) {
alert("Are you stupid? how can you type your name wrong. you idiot. fix it, dont call me for this.");
return false;
}

[a-zA-Z] detect only english. they contain unicode characters and so it will not detect it.
Now check it
var a="André,René,Fran çois,Anne-Marie,éç,é-çé ç,Éric,Hélène".split(",");
var myNameFilter = /^([a-zA-Z\-éçèàùâêîôûëïüÿçÉ\s]+)$/;
i=0;
while(i<a.length){
console.log(a[i]+"->"+myNameFilter.test(a[i]));
i++;
}

Related

Validate an email address always fails [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 3 years ago.
Please see the code below:
function (string regex, string value)
{
var regularExpression = new RegExp(regex);
return regularExpression.test(value);
}
Why does it fail? It seems to fail for everything I enter. I got the code from here: How to validate an email address in JavaScript? i.e. the community answer that starts: "I've slightly modified Jaymon's answer".
Try this
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}

Need assistance with regex in javascript [duplicate]

This question already has answers here:
What is a regular expression which will match a valid domain name without a subdomain?
(23 answers)
Closed 6 years ago.
I would like to validate url's in javascript before user proceeds further.
The urls below should match;
http://google.com
http://www.google.com
www.google.com
google.com
And not match;
http://google
http://www.google
www.google
google
Please help me im really bad at regex
try like this
var urlR = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)
(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var url= content.match(urlR);
or
var regex = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.#:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?");
if(regex.test("http://google.com")){
alert("Successful match");
}else{
alert("No match");
}

Regex and Javascript : find word with accents [duplicate]

This question already has answers here:
How can I use Unicode-aware regular expressions in JavaScript?
(11 answers)
Closed 8 years ago.
I try to find and stock in HTML paragraphe all the word.
Actually, I have a function like this
p.html(function(index, oldHtml) {
return oldHtml.replace(/\b(\w+?)\b/g, '<span>$1</span>');
});
But it's only return word without accent.
I test on regex101.com
https://www.regex101.com/r/jS5gW6/1
Any idea ?
Use a character class:
oldHtml.replace(/([\wàâêëéèîïôûùüç]+)/gi, '<span>$1</span>');
Trying it:
var oldHtml = 'kjh À ùp géçhj ùù Çfg';
var res = oldHtml.replace(/([\wàâêëéèîïôûùüç]+)/gi, '<span>$1</span>');
gives
"<span>kjh</span> <span>À</span> <span>ùp</span> <span>géçhj</span> <span>ùù</span>
Çfg"

Regex to validate an email address [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 8 years ago.
I am not expert in JavaScript and need to get this regex to work:
function validateEmail(email) {
var re = /[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,22}/;
return re.test(email);
}
Currently this doesn't work fine, even for myemail#hotmail.com.
I don't need a new regex, just few changes to this one to get it to work.
You need to use the case-insensitive flag, i:
var re = /[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,22}/i;
Without this, it would only match upper-case Latin letters, e.g. MYEMAIL#HOTMAIL.COM.
See MDN for a list of supported flags.

Validation in javascript not working [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
The below code is for validating email. I am getting false for inputs like,
mike#gmail.com
kid#gmail.com
stain#yahoo.com
Can someone point what mistake in the code?
function validate(){
fieldValue = document.getElementById("check").value;
pattern = new RegExp(/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/);
if(pattern.test(fieldValue)){
alert("true");
} else {
alert("false");
}
}
Thanks
A-Z only checks capital letters. Add also a-z:
[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}
Using RegEx to validate email addresses is difficult.
However, the issue with your code is the casing (as others have pointed out). You can fix it by changing A-Z to A-Za-z, which will check for lowercase and capital letters.
function validate(){
fieldValue = document.getElementById("check").value;
pattern = new RegExp(/^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/);
if(pattern.test(fieldValue)){
alert("true");
} else {
alert("false");
}
}
For validating email addresses, this pattern has worked for me for quite some time:
/^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/

Categories

Resources