我不知道它是如何在片段中工作的! :( 我检查了很多次。在我的输出中,即使电话号码错误,密码字段也会聚焦。
function checkmatch() {
var name = $("#username").val();
var address = $("#address").val();
var email = $("#emailid").val();
var mobile = $("#phonenum").val();
var password = $("#newPwd").val();
var cnfirmPassword = $("#confirmPwd").val();
if (password != cnfirmPassword) {
alert("Passwords do not match.");
$("#newPwd").val('').focus();
$("#confirmPwd").val('');
return false;
} else if ((name == null || name == "") || (address == null || address == "") || (email == null || email == "") || (mobile = null || mobile == "") || (password == null || password == "") || (cnfirmPassword == null || cnfirmPassword == "")) {
alert("Please fill all the required fields.");
return false;
} else {
/*$.ajax(
{
type: "POST",
url: "assmt1.php",
datatype: "html",
data: $("#fm1").serialize(),
success: function(response)
{
}
});
alert("Your form has been submitted. Thank you!");*/
}
}
function validateMobile() {
var phoneno = $('#phonenum').val();
var phone = document.forms["fm1"]["field4"].value;
var phonenumber = phone.replace(/[^\d]+/g, "");
if (phonenumber.length > 9 && phonenumber.length < 13) {
return true;
} else {
alert("Enter a valid phone number");
$("#phonenum").val('').focus();
return false;
}
}
function isnumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if ((charCode > 31 && charCode < 43) || (charCode > 43 && charCode < 48) || (charCode > 57))
return false;
return true;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="fm1" name="myForm" method="post">
<label>Mobile Number <input type="text" id = "phonenum" name="field4" onkeypress="return isnumberKey(event);" onchange = "validateMobile();" /></label>
<label>Password <input type="password" id="newPwd" name="field5" /></label>
<label>Confirm Password <input type="password" id="confirmPwd" name="field6"/> </label>
<input style="cursor:pointer" type="button" value="Submit" onclick="checkmatch();" name="Sign Up" />
&#13;
这是我的电话号码验证代码。我希望清除phonenumber字段,然后在验证失败时聚焦。
if (phonenumber.length > 9 && phonenumber.length < 13) {
return true;
} else {
alert("Enter a valid phone number");
$("#phonenum").val('').focus();
return false;
}