manick Posted April 12, 2006 Share Posted April 12, 2006 Hi all,I have to validate three form fields on the fly, i thought about using onblur but it blobs it!!! how do you go about validating fields on more than one field without it going into an endless loop, here is the code i am using so you can see the problem i am encountering:<script>var regexp=/^[A-Z]{1,2}\d{1,2}\s{0,}\d[A-Z]{2}$/ifunction chkPC(shipPostCode){if (!regexp.test(shipPostCode.value)) {shipPostCode.value="Please enter PostCode";shipPostCode.focus();return false}return true}</script><script>function chkAddy(shipDelivery) {if ((shipDelivery.value=="Enter the delivery address.")||(shipDelivery.value=="")||(shipDelivery.value=="Please enter your correct Shipping Address")) {shipDelivery.value="Please enter your correct Shipping Address";shipDelivery.focus();return false}else{return true;}}</script><script>var cardexp=/^\d{4}\s{0,}\d{4}\s{0,}\d{4}\s{0,}\d{4}$/function chkCard(billCardNumber){if (!cardexp.test(billCardNumber.value)) {billCardNumber.value="Please enter your credit card number";billCardNumber.focus();return false}return true}</script>Im using onblur, but obviously as the user tries to go onto the next field (which also has onblur) if they have filled it out incorrectly, the browser locks.Any help much appreciated!!Manick Link to comment Share on other sites More sharing options...
pulpfiction Posted April 12, 2006 Share Posted April 12, 2006 <script>function validate(){var cardexp=/^\d{4}\s{0,}\d{4}\s{0,}\d{4}\s{0,}\d{4}$/var regexp=/^[A-Z]{1,2}\d{1,2}\s{0,}\d[A-Z]{2}$/iif (!regexp.test(document.frmname.fldname.value)) {document.frmname.fldname.value="Please enter PostCode";document.frmname.fldname.focus();return false}if ((document.frmname.fldname.value=="Enter the delivery address.")||(document.frmname.fldname.value=="")||(document.frmname.fldname.value=="Please enter your correct Shipping Address")) {document.frmname.fldname.value="Please enter your correct Shipping Address";document.frmname.fldname.focus();return false}if (!cardexp.test(document.frmname.fldname.value)) {document.formname.fldname.value="Please enter your credit card number";document.frmname.fldname.focus();return false}}</script>Use the function validate()Example:<form name="frmname" onSubmit="return Validate()"><input class="textbox" type="Text" name="fldname"Try this, when there is some error it will come back to the same form, when the validation is successful then it can redirect to another page.... Link to comment Share on other sites More sharing options...
manick Posted April 13, 2006 Author Share Posted April 13, 2006 Hi there,thanks for that, but unfortunately, the remit says that I have to validate as the user enters data rather than on submit (I have already had to show that I can perform on submit) , therefore that is the problem i am having with the onblur event. Is there a better way handler than onblur that will not return an error, but will force the user to enter data (i.e. onchange won;t do what I need it to do.TaNick Link to comment Share on other sites More sharing options...
Webworldx Posted April 13, 2006 Share Posted April 13, 2006 Why not make them enter the data in sequential order? Like: <form id="myForm"><input name="myField1" onBlur="blurHandler();" value="" /><br /><input name="myField2" onBlur="blurHandler();" value="" /><br /><input name="myField3" onBlur="blurHandler();" value="" /><br /></form><script type='text/javascript'> function blurHandler(){ with(document.getElementById('myForm')){ if(myField1.value==""){ myField1.focus(); return; } if(myField2.value==""){ myField2.focus(); return; } if(myField3.value==""){ myField3.focus(); return; } } }</script> You could also add "Field 2 has not been completed. Would you like to edit it now?" style error messages in so users know what's happening. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now