0

想用一个 javascript 函数验证 html 表单。我有表单验证,仅验证该字段是否为空。我想检查电子邮件是否正确以及电话号码是否为数字。现在表单打开弹出警报,但在输入电子邮件后它会停止。

这是我的表单代码

<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>

这是 Javascript 代码:-

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 );
}
4

4 回答 4

2
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;
}

此功能将帮助您验证电话和电子邮件地址

于 2013-10-28T23:02:29.343 回答
1

伙计们,最后我有一个很好的正则表达式,用于使用单个输入字段验证电子邮件和电话号码。我希望它会有所帮助。

谢谢

^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})|(^[0-9]{10})+$

演示:

https://jsfiddle.net/sanat/b2m436L5/18/

于 2021-06-21T13:55:17.447 回答
0

我写了这个,你可以检查一下

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 );
    }
于 2013-10-29T06:54:52.437 回答
0

在这里我找到了一些有用的链接,用于使用 Javascript 验证手机号码和电子邮件

手机号码验证

使用 Java Script 验证手机号码 -带演示

<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>

电子邮件验证

使用带有演示的Java 脚本进行电子邮件验证

 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 ");
        }

    }
于 2015-04-29T14:32:51.090 回答