admin 管理员组文章数量: 1086019
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 );
}
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 );
}
Share
Improve this question
asked Oct 28, 2013 at 22:30
Saransh KaliaSaransh Kalia
11 gold badge1 silver badge3 bronze badges
1
-
Validating email addresses is already well covered here on Stack Overflow. Validating phone numbers is a mug's game. People want to put
(
and)
and+
and-
and.
andx
(and others) in their phone numbers. Cope. – T.J. Crowder Commented Oct 28, 2013 at 22:37
4 Answers
Reset to default 2function 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
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/sanat/b2m436L5/18/
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 );
}
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 ");
}
}
本文标签: phpJavascript Email and Phone ValidationStack Overflow
版权声明:本文标题:php - Javascript Email and Phone Validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744082938a2530591.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论