Reputation: 1
Want to validate html form with one javascript function. I have the form validation, thats only validating, if the field is empty or not. I want to check if the email is correct and the phone number is numeric. Right now the form open popup alert but after entering email it stops.
Here are my form codes
<form name="appointment" method="post" onsubmit="return doValidate();">
Name:*<br />
<input type="text" name="requiredname" />
Email:*<br />
<input type="text" name="requiredemail" />
Phone:*<br />
<input type="text" name="requiredphone" />
Appointment Date:*<br />
<input type="text" name="requireddate" />
Appointment Time:*<br />
<input type="text" name="requiredtime" /><br /><br />
<input name="submit" type="submit" value="Book Now" /> </form>
This is the Javascript codes:-
function doValidate()
{
if (document.appointment.requiredname.value =="")
{
alert("Please put your name");
document.appointment.requiredname.focus();
return false;
}
var readmail = document.appointment.requiredemail;
var checkatsymbol = readmail.indexof("@");
var checkdotsymbol = readmail.lastindexof(".");
if (readmail.value =="" || checkatsymbol<1)
{
alert("Please put the correct email address");
document.appointment.requiredemail.focus();
return false;
}
if (document.appointment.requiredphone.value =="" )
{
alert("Please put your phone");
document.appointment.requiredphone.focus();
return false;
}
if (document.appointment.requireddate.value =="" )
{
alert("Please put your appointment date as DD/MM/YYYY");
document.appointment.requireddate.focus();
return false;
}
if (document.appointment.requiredtime.value =="")
{
alert("Please put your appointment time as HH:MM AM/PM");
document.appointment.requiredtime.focus();
return false;
}
return ( true );
}
Upvotes: 0
Views: 21030
Reputation: 1154
guys Here final I have a good regex for validating email and phone number with the single input field. I hope It's helpful.
Thanks
^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})|(^[0-9]{10})+$
Demo :
https://jsfiddle.net/sanat/b2m436L5/18/
Upvotes: 1
Reputation: 439
Here i found some usefull links, which is used to validate Mobile number and Email Using Javascript
Mobile Number Validation
Mobile number Validation using Java Script - with Demo
<input id="txtPhoneNo" type="text" onkeypress="return isNumber(event)" />
<input type="button" value="Submit" onclick="ValidateNo();">
<script type="text/javascript">
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert("Please enter only Numbers.");
return false;
}
return true;
}
function ValidateNo() {
var phoneNo = document.getElementById('txtPhoneNo');
if (phoneNo.value == "" || phoneNo.value == null) {
alert("Please enter your Mobile No.");
return false;
}
if (phoneNo.value.length < 10 || phoneNo.value.length > 10) {
alert("Mobile No. is not valid, Please Enter 10 Digit Mobile No.");
return false;
}
alert("Success ");
return true;
}
</script>
Email Validation
Email Validation using Java Script with Demo
function validateEmail() {
var email = document.getElementById('txtEmail');
var mailFormat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (email.value == "") {
alert( " Please enter your Email Id ");
}
else if (!mailFormat.test(email.value)) {
alert( " Email Address is not valid, Please provide a valid Email ");
return false;
}
else {
alert(" Success ");
}
}
Upvotes: 0
Reputation: 1
I wrote this, can u check
function doValidate()
{
if (document.appointment.requiredname.value =="")
{
alert("Please put your name");
document.appointment.requiredname.focus();
return false;
}
var readmail = document.appointment.requiredemail.value;
var checkatsymbol = readmail.indexof("@");
var checkdotsymbol = readmail.lastindexof(".");
if (checkatsymbol < 1 || checkdotsymbol+2>=readmail.length )
{
alert("Please put the correct email address");
document.appointment.requiredemail.focus();
return false;
}
if (document.appointment.requiredphone.value =="" )
{
alert("Please put your phone");
document.appointment.requiredphone.focus();
return false;
}
if (document.appointment.requireddate.value =="" )
{
alert("Please put your appointment date as DD/MM/YYYY");
document.appointment.requireddate.focus();
return false;
}
if (document.appointment.requiredtime.value =="")
{
alert("Please put your appointment time as HH:MM AM/PM");
document.appointment.requiredtime.focus();
return false;
}
return ( true );
}
Upvotes: 0
Reputation: 2170
function validateEmail(email) { //Validates the email address
var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return emailRegex.test(email);
}
function validatePhone(phone) { //Validates the phone number
var phoneRegex = /^(\+91-|\+91|0)?\d{10}$/; // Change this regex based on requirement
return phoneRegex.test(phone);
}
function doValidate() {
if (!validateEmail(document.appointment.requiredphone.value) || !validatePhone(document.appointment.requiredphone.value) ){
alert("Invalid Email");
return false;
}
This functions would help you to validate for phone and email address
Upvotes: 2