javascript - focus() is not working in my html form -
i dont how working in snippet! :( checked many times. in output, password field getting focused when phone number wrong.
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 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 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 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; }
<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" />
this code phonenumber validation. want phonenumber field cleared , focused if validation fails.
if (phonenumber.length > 9 && phonenumber.length < 13) { return true; } else { alert("enter valid phone number"); $("#phonenum").val('').focus(); return false; }
Comments
Post a Comment