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
}
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 am working on Angular 5 Reactive Form validation and trying to validate UK PostCode using custom validation function which is working and testing apart from in case provide extra letter or numeric value at end of postcode 2nd part, it validate true, for example NW10 5NW is correct but if I type anything like NW10 5NWRRRRRRRRRRRRRRRR is also return true which is not correct.
I have tried following regular experssion on https://regexr.com/ and it return correct response, not sure why in javaScript not behaving same way???
function postCodeValidator(control: FormControl)
{
let givenPostCode = control.value;
let UKPostCodePattern = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})/;
var isUKPostCodeValid = UKPostCodePattern.test(givenPostCode);
console.log("postcode validity ",isUKPostCodeValid, " for ", givenPostCode);
if(!isUKPostCodeValid)
{
return {
postCode:{
required:"UK Valid PostCode",
provided: givenPostCode
}
}
}
return null;
}
Try using following regex
^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2}))$
https://regexr.com/3pp3r
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;
}
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
}
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;
}