使用php在单输入类型中进行电子邮件和手机号码验证

时间:2016-09-25 07:38:30

标签: php validation

<form action=demo.php method="post">
<div >
        <label class="control-label" for="email">Your email address/Mobile number</label>
        <div class="controls">
            <input type="email"  name="email" required />

        </div>
    </div>

    <input type="submit" name="submit">Submit</button>
</form>

我希望通过PHP验证验证电子邮件或移动设备,使用preg进行单输入,而Mobile仅启动7,8&amp; 9只应该是10位数。 我怎样才能在PHP中验证这一点。

1 个答案:

答案 0 :(得分:0)

对于javascript

function validateEmail() {
    var email = document.getElementById('txtEmail');
    var mailFormat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})|([0-9]{10})+$/;
    if (email.value == "") {
        alert( "  Please enter your Email or Phone Number  ");
    }
    else if (!mailFormat.test(email.value)) {
        alert( "  Email Address / Phone number is not valid, Please provide a valid Email or phone number ");
        return false;
    }
    else {
        alert(" Success ");
    }
}

将此正则表达式与Node JS一起使用

  

/ ^([_ A-Z0-9] +([_ A-Z0-9] +)的 @ [A-Z0-9 - ] +([A-Z0-9 - ]。+) )([AZ] {2,5-})|(\ d +)$ /

Demo