我的目的是用户只输入电子邮件或手机号码。我在 OR 逻辑中通知用户的代码部分成功
但是......如果用户想把这两个细节都给我电子邮件和手机号码。并错误地输入了该信息,然后他/她将通知该信息以更新数据。
<form action="index.php" method="post">
<label for="email">Your E-Mail Address *</label>
<input id="UserEmail" type="text" name="email" maxlength="80" size="30"/>
</p><p>
Or
</p><p>
<label for="mobile">Mobile Number *</label>
<input id="mobile" type="text" name="mobile" maxlength="50" size="30" /></p>
下面是jquery代码
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#UserEmail").val();
// email & mobile input cheak
if(emailaddressVal == '' && $("#mobile").val() == '') {
$("#UserEmail").after('<p><span class="error">Please enter your email address.</span></p>')
$("#mobile").after('<p><span class="error">Please enter your Mobile number.</span></p>')
hasError = true;
}
// validating currect email
else if(!emailReg.test(emailaddressVal)) {
$("#UserEmail").after('<p><span class="error">Enter a valid email address.</span></p>')
hasError = true;
}
// Mobile number
else if( $("#mobile").val()) {
$("#mobile").after('<p><span class="error">Enter currect Mobile number.</span></p>')
hasError = true;
}