User-1330468790 posted
Hi SSK_Nani,
You could simply use Regex to complete the validation as long as you have an allow-list for the domain extensions.
var EMAIL_REGEX = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.((com)|(org)|(co.in)|(net))$/;
If you really want to validate the Email Domain extensions using JQuery, you might mean using JQuery Validation plugin. You will still need to set up your own email validation function as custom email.
$.validator.addMethod("customemail",
function(value, element) {
return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.((com)|(org)|(co.in)|(net))$/.test(value);
},
"Sorry, the domain extension is not allowed."
);
$("#input").validate({
rules: {
Email: {
required: true,
customemail: true
}
},
});
Below is a demo that proves how you could use the Regex.
html:
<input id="emailAddress" type="text" >
<input type="button" value="Check" onclick="emailCheck()">
<label id="result" ></label>
Javascript:
var EMAIL_REGEX = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.((com)|(org)|(co.in)|(net))$/;
function emailCheck()
{
var email = document.getElementById("emailAddress").value;
document.getElementById("result").innerText = EMAIL_REGEX.test(email) == true ? 'valid' : 'invalid'
}
You could test the codes in this jsfiddle.
Hope this can help you.
Best regards,
Sean