andrewgarn Posted July 9, 2008 Share Posted July 9, 2008 I have done a bit of this before but just to check the length of a field or field present, bit rusty on it now. I want to validate a username field and bring an error if the username contains a space or a hyphen. So far I have: <form name="register" onsubmit="return validateUsername(this)" method="post" action="register.php"> <input type="text" name="username"> <input type="password" name="password"> <input type="text" name="email" maxlength="100"> <input type="submit" name="submit" value="Submit"> </form> <script type="text/javascript"> function validateUsername(fld) { var error = ""; var illegalChars = [something here] if (illegalChars.test(username)) { fld.style.background = 'Yellow'; error = "The username is invalid, you must use an underscore to represent a space.\n"; return error; } </script> Help would be great. Thanks Quote Link to comment Share on other sites More sharing options...
andrewgarn Posted July 9, 2008 Author Share Posted July 9, 2008 Done it server side with php now if(substr_count($username," ")>0) {//invalid} Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 10, 2008 Share Posted July 10, 2008 well if you still wanted a javascript check as well, use the indexOf method if ( str.indexOf('badchar') != -1 ) { //Stuff } Quote Link to comment Share on other sites More sharing options...
adam84 Posted July 10, 2008 Share Posted July 10, 2008 Change the submit button to a regluar button, and add a onclick event to that button. Then use javascript to validate whatever you want. Then if everything is good, just submit the form with javascript Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 10, 2008 Share Posted July 10, 2008 just remember, always use php as well as javascript when checking user data, as javascript can be turned off... Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 11, 2008 Share Posted July 11, 2008 Change the submit button to a regluar button, and add a onclick event to that button. Then use javascript to validate whatever you want. Then if everything is good, just submit the form with javascript Adam, I'm not mening to bash you, but that is the exact opposite of how you should perform validation. The reason is that if the user does not have JS enabled they are unable to use the form at all. You should ALWAYS be validating on the server-side anyway. But, having validation client-side is a nice feature to have as well. You just don't weant to make your site unusable for non-js users. You can elegantly support both with the following method. Use a standard submit button, and in the FORM tag include validation through an onsubmit trigger such as onsubmit="return validate()". The validate() function should return false if validation fails and the form won't be submitted. That way if a user has JS enabled they will get the benefit of client-side validation. However, if a user does not have JS enabled the form will still submit and any errors will be taken care of in server-side validation. Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 12, 2008 Share Posted July 12, 2008 @mjdamato Good way of expanding my post into three paragraphs 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.