Validation in javascript not working [duplicate] - javascript

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

Related

Form validation name input issue with whitespace? [duplicate]

This question already has answers here:
Regular expression for first and last name
(28 answers)
Closed 2 years ago.
So I have this regex to confirm if a name is valid or not, but if I were to add spaces before the name, it will display my error message.
Is there a way to convert my code to validate names like " Bob Joe" if there's extra spaces in the front? But I also wanna make sure nobody can just type "a b joe b bob" and still get a valid name.
Here's my current validation code
if (!values.name) {
errors.name = 'Name required';
} else if (!/^[A-Za-z]+/.test(values.name)) {
errors.name = 'Enter a valid name';
}
Please try string.trim() function to validate your input.Please click link to better understand https://www.w3schools.com/jsref/jsref_trim_string.asp#:~:text=The%20trim()%20method%20removes,not%20change%20the%20original%20string.

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());
}

Why the name validation is invalid? [duplicate]

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++;
}

How to make regex for phone number start with 60xxxxxxx [duplicate]

This question already has answers here:
Javascript regex phone validation
(3 answers)
Closed 7 years ago.
sorry i know this question might duplicated but i'm really bad in regex, hope someone could help to fix it. How i can apply this regex in my javascript. Any help will be appreciated.
^6?01\d{8}$
My current coding.
function phoneNumber()
{
var mobile = document.mainform.HP_NO.value;
//need condition here if phone number does not match with the regex given
alert("Not a valid Phone Number.");
document.mainform.HP_NO.value = "";
document.mainform.HP_NO.focus();
return false;
///
return true;
}
<input type="text" class="input-name" name="HP_NO" placeholder="e.g (60121234567)" onblur="if(value=='') value = '';phoneNumber();" onfocus="if(value=='') maxlength="11" onPaste="return false"/>
How to make regex for phone number start with 60xxxxxxx
Your regex should be
^60\d{8}$
to check if number starts with 60
Explanation:
^: Starts with
60: Matches literal 60
\d: Matches any number/digit
{8}: Matches 8 times the previous group
$ : Ends with
Visual Representation
How i can apply this regex in my javascript.
You can use test() to check if string satisfies regex.
var regex = /^60\d{8}$/;
if (regex.test(mobile) === false) {
alert("Not a valid Phone Number.");
....
}

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.

Categories

Resources