Use javascript to determine email address is valid or not [duplicate] - javascript

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 6 years ago.
I have 6 email address like below:
(1) ali#hotmail.com -> valid
(2) #hotmail.com -> invalid
(3) ali#hotmail.com, abu#hotmail.com -> valid
(4) ali#hotmail.com, #hotmail.com -> invalid
(5) ali#hotmail.com, abu#hotmail.com, ahmad#hotmail.com -> valid
(6) ali#hotmail.com, #hotmail.com, ahmad#hotmail.com -> invalid
How do I use JavaScript to determine that the email address is in full format?
I try startsWith("#hotmail.com"), endsWith("hotmail.com"), indexOf("#hotmail.com") also cannot fulfill all the email addresses above.
Can someone help me?

Using regular expressions is probably the best way. Pass the string in this function and this function will return true of false
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(email);
}

You could use regex
For example,
/[A-Za-z0-9-]+#hotmail.com/

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

Getting a true or false reply to a Regex match? [duplicate]

This question already has answers here:
Return true/false for a matched/not matched regex
(5 answers)
Closed 3 years ago.
I'm trying to match an entire string against a regex formula. This is for validating if a phone number field is likely correct (just based on allowed characters, anyone can make up an number). I've played with Regex before but never truly understood the nuances that make it powerful.
Below I have my dummy phone number and I have the regex I'm using. As you can see I'm simply comparing the length of the match vs the length of the string and if they match the number must be valid.
Is there a way to get a simple true/false reply from a Regex check on an entire string?
var num = '+1 (888) 456-7896';
var regex = /[0-9+ ()-]*$/;
var found = num.match(regex);
console.log(found[0].length);
console.log(num.length);
You can use test()
var found = regex.test(num);

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