I had created an registration form in which i had created and email i had provided validation to all the filed but I'm confused how to give validation to email field because i want that email should be either gmail.com or yahoomail.com if some one enters any other email even yahoomail.co.in it should give error message.
here is the code i which checking that its having # and . in the email or not
var atdrate=email.indexOf("#");
var dot1=email.indexOf(".");
else if(atdrate<1 || dot1<1)
{
alert("Enter valid Email");
if(gml<atdrate+1)
alert("Enter a vaild mail id");
else if(yml<atdrate+1)
alert("Enter a valid email id");
}
Using Regular Expressions is probably the best way. Here's an example (live demo):
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);
}
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
*answer from https://stackoverflow.com/a/46181/1521984
Update: Then use the JavaScript split() function to split the mail address after the '#' and check the value versus your strings.
var mail = yourMailAdress.split("#");
if (mail[1] == "gmail.com" || mail[1] == "yahoomail.com") {
// OKAY
} else {
// false
}
Related
I am trying to validate inputText to make sure it is a correctly formatted email address. However, I'm getting the following issues:
let reg = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (reg.test(emailText) === false) {
alert('Email is Not Correct');
// this.setState({email: emailText});
return false;
} else {
// this.setState({email: emailText});
alert('Email is Correct');
}
Works with some issues the following:
hello#gmail.com // works
hello.world#gmail.com // works
some.thing#gm.ci.co // will not work and is a real email address
Another other issue is that my characters count is limited. How do I remove the limit? I need to be able to have unlimited characters
I have coded not empty on fields. how do i check that email is valid or either mobile number is valid one that too us phone number.
if(mob_or_email==""){
document.getElementById('busp_email').innerHTML="Mobile/Email required";
$("#busp_email").removeClass('field_validation_error hidden');
$("#busp_email").addClass('field_validation_error');
$("#busi_name").css("color","#f42156");
}
if($('#login_password').val()==""){
document.getElementById('logp_pwd').innerHTML="Password required";
$("#logp_pwd").removeClass('field_validation_error hidden');
$("#logp_pwd").addClass('field_validation_error');
$("#log_pwd").css("color","#f42156");
}
If I understand your request correctly, you may have an e-mail or a phone number in the same field?
You will need regular expressions for this (If my suggestions are not satisfying, search trough the internet, there are many samples for specific cases)
function validateEmail(email) {
var re = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(email);
}
function validatePhone(phone) {
var re = /^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/;
return re.test(phone);
}
With these functions you can validate e-mail and us phone numbers.
just use
if (validateEmail(mob_or_email) or validatePhone(mob_or_email)) {
//is either a valid email or (us) phone number
}
I've learned to make the login form and I planning to validate two values ( email and password ) and use it as a condition true or false .
i have code like this
function userValid(email) {
var usr = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$\i/ ;
return usr.test(email);
};
function passValid(password) {
var passw = /^[A-Za-z]\w{7,14}$/;
return passw.test(password);
};
the code above it use to validating email and password in my form and i think if i passing a value to that function (for example passing value email to userValid() function) it will return true/false.
i plan to create validating form or login form that first check the user email & password to be valid before i submit (lets say to the server).
and i think the logic is like this :
if both values is TRUE then Login Success
if one of the values is TRUE then Login Failed
if both values is FALSE then Login Failed
else (the value must be EMPTY or NULL) then Please Insert Email and Password
and so on
my question is how to create conditional statement with ABOVE case, and of course in JAVASCRIPT :) thanks.
I don't know how you acces the email and the password, but this should work if you fill those in.
if ( userValid(email) == true && passValid(password) == true ) {
//Login succes code
}
else if ( userValid(email) == false || passValid(password) == false ){
//Login failure code
}
else {
//The "Please insert" code
}
The && operater (and) returns true only if both values are true.
The || operater (or) returns true if at least one of the values is true.
This can probably be done easier, but it should work like this.
You can add code to check if the password and the username are empty, but I don't think that's necesarry.
So what you want to do is create a function to be called when the user tries to submit your form. If this function returns false, the form will not submit. Otherwise it submits the form. You could write it like this:
var yourForm = document.getElementById("yourForm");
var yourEmail = document.getElementById("yourEmail");
var yourPassword = document.getElementById("yourPassword");
var yourWarningLabel = document.getElementById("yourWarningLabel");
yourForm.onsubmit = function() {
if (yourEmail.value == "" || yourPassword.value == "") { //Check if the fields have something in them
yourWarningLabel.innerHTML = "Please Insert Email and Password";
return false;
} else return (userValid(yourEmail.value) && passValid(yourPassword.value));
};
Where yourWarningLabel is a <p>, <label> or other text tag that you use to display the warning.
The double pipes || represent an OR operator, meaning that if the email is empty OR the password is empty we should display a warning.
The double ampersands && represent an AND operator, meaning if the email passes AND the password passes the check then we return true and submit the form, otherwise it returns false.
In addition to this you should also be checking and cleaning input on your server side or in the page that uses the input.
I am trying to write a function in Javascript to validate email address. Here is the function.
function validateEmailAddress() {
var patternForEmail = /^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/;
var regexPatternForEmail = new RegExp(patternForEmail, 'i');
// Email address and the Confirm Email address values should match
if ($('#txtEmail').val() != $('#txtConfirmEmail').val()) {
$('#dvErrorMsg').html("Email addresses do not match.");
$('#txtEmail').focus();
return false;
}
else if (!regexPatternForEmail.test($('#txtEmail').val())) {
$('#dvErrorMsg').html("Please enter a valid email address.");
$('#txtEmail').focus();
return false;
}
else
return true;
}
The problem here is I am getting an error, 'Syntax error in regular expression' during RegExp object instantiation.
I tried debuggin in IE 11 and that's where i found the error.
Could someone please suggest me a solution for this.
Screen shot taken while debugging:
You don't need to create another regex variable using RegExp constructor. Just use only the below.
var patternForEmail = /^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/i;
i at the last called case-insensitive modifier which helps to do a case-insensitive match.
Example:
> var patternForEmail = /^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/i;
> patternForEmail.test('foo#bar.com')
true
> patternForEmail.test('#foo#bar.com')
false
In most of the times this kinds of errors occurs because of browser compatibility , so always we need to use the codes which can be run in all the browsers. I hope the following changes will help you.
var patternForEmail = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
then you can match the expression using
if (patternForEmail.test($('#txtEmail').val())) {
$('#dvErrorMsg').html("Please enter a valid email address.");
$('#txtEmail').focus();
return false;
}
or else you can use match() function also which will be more flexible for all the browsers.
var email = $('#txtEmail').val();
if (!email.match(/^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
$('#dvErrorMsg').html("Please enter a valid email address.");
$('#txtEmail').focus();
return false;
}
This is part of my jQuery script. I need to make the system validate emails for a specific domain.
like example#schooldomain.com
And only allow emails from #schooldomain.com
Code:
email: function(value,element){return this.optional(element)||/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);}
Firstly, as pointed out in the comments, validate the email using regex, and then check if the email is from the right domain.
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,}))$/;
if(re.test(email)){
//Email valid. Procees to test if it's from the right domain (Second argument is to check that the string ENDS with this domain, and that it doesn't just contain it)
if(email.indexOf("#thedomain.com", email.length - "#thedomain.com".length) !== -1){
//VALID
console.log("VALID");
}
}
}
Thanks to this thread I found another solution for only accepting one specific domain after the "at" / "#". Get everything after the dash in a string in JavaScript
Basically dividing the email in two, the text before # and the text after #. If the text after # is not equal to the specified domain the validation will be false.
// Email validation
let em = document.forms['Login']['email'].value;
let atpos = em.indexOf("#");
let domain = em.split("#")[1]; // Saves user input after the # (at)
if (em == null || em == "") {
alert("Email can not be empty.");
document.getElementById('e').focus();
return false;
}
// First test checks for atleast one character before #
else if (atpos < 1 || domain != "gmail.com"){ // Second test checks if the user entered a gmail.com domain after #
alert("Not a valid e-mail address. Please write your gmail address like this: username#gmail.com.");
document.getElementById('e').focus();
return false;
}