jwwceo Posted December 13, 2007 Share Posted December 13, 2007 This may be a Javascript question...but I am curious what the best way to validate data is? I mean things like making sure a phone number is correct, or that the double entry passwords match. I could do this is php but I seem like I would need a page refresh. I am looking for something that maybe throws one of those little javascript error boxes??? Thanks for any tips!! James Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 13, 2007 Share Posted December 13, 2007 This is really easy with javascript <script type="text/javascript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") { alert(alerttxt); return false; } else { return true; } } } function field_size(field,limit,alerttxt){ with(field) { if(value.length > limit){ alert(alerttxt); return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(title,"Error Message:\n All fields must be filled out!")==false) { title.focus(); return false; } else if(validate_required(comment,"Error Message:\n All fields must be filled out!")==false) { comment.focus(); return false; } else if(validate_required(swf,"Error Message:\n All fields must be filled out!")==false) { swf.focus(); return false; } else if(field_size(title,28,"Error Message:\n Title length is more than 23 chars!")==false) { title.focus(); return false; } else if(field_size(comment,200,"Error Message:\n Comment length is more than 200 chars!")==false) { comment.focus(); return false; } else if(field_size(password,50,"Error Message:\n password length is more than 50 chars!")==false) { password.focus(); return false; } } return true; } </script> Heres an example of one I wrote. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 13, 2007 Share Posted December 13, 2007 You must validate data when it reaches the server. You can validate data using javascript in the browser before it is submitted to the server as a service to your visitor. When your php code validates the data, you need to build up an error message (most people use an array to hold multiple validation error messages.) Then if there were any validation errors, you output those along with the original form (and any existing data in the form fields so that it does not need to be entered again.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.