doumaoao0182 2013-10-28 22:30
浏览 111

Javascript电子邮件和电话验证

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 );
}
  • 写回答

3条回答 默认 最新

  • dqwyghl0649 2013-10-28 23:02
    关注
    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

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路