Email
by Ankit Patidar
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div>
<label>Email</label><br />
<input type="email" id="email">
<button id="btnEmail">Email Check</button>
</div>
<div> </div>
<div>
<label>Phone Number</label><br />
<input type="tel" id="phone">
<button id="btnPhone">Phone Check</button>
</div>
<div> </div>
<div id="error"></div>
JavaScript
function checkValidate(caller, val){
console.log("hello");
if(caller == "email"){
if(!val){
$("#error").text("Provide email");
} else{
const emRegex = /^(([^<>()[\]\\.,;:\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 emRegex.test(val);
}
}
else if(caller == "phone"){
if(!val){
$("#error").text("Provide phone");
} else{
const pnRegex = /^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
//var patt = /^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/;
return pnRegex.test(val);
}
}
}
$("#btnEmail").on("click", () => {
checkValidate("email", $("#email").val());
});
$("#btnPhone").on("click", () =>{
checkValidate("phone", $("#phone").val());
});