galvin Posted June 7, 2009 Share Posted June 7, 2009 Anyone know how to add some javascript to prevent a user from typing a "space" as the FIRST character (in a simple text field)? In other words, if the user accidentally hits the space key before typing anything else, it will be essentially be ignored. (I know there are ways to strip a leading space after the text is submitted, but I need it to not even get to that point for reasons I won't bore you with). Any help/ideas would be greatly appreciated. -Greg Quote Link to comment https://forums.phpfreaks.com/topic/161290-prevent-a-space-from-being-first-characted-typed-in-a-text-field/ Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 You can have a onkeyup function that strips it. Quote Link to comment https://forums.phpfreaks.com/topic/161290-prevent-a-space-from-being-first-characted-typed-in-a-text-field/#findComment-851104 Share on other sites More sharing options...
Adam Posted June 7, 2009 Share Posted June 7, 2009 Or you could validate on the 'onblur' event. I'd probably just use one of the many custom trim functions/prototypes available online, something like this (found here): <script type="text/javascript"> String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); } </script> <input type="text" name="..." onblur="this.value = this.value.trim();" /> You may wish to switch to the 'onkeyup' event if it needs to be validated after every character is input. There's load of other ways you could do it as well... Quote Link to comment https://forums.phpfreaks.com/topic/161290-prevent-a-space-from-being-first-characted-typed-in-a-text-field/#findComment-851124 Share on other sites More sharing options...
.josh Posted June 7, 2009 Share Posted June 7, 2009 Anyone know how to add some javascript to prevent a user from typing a "space" as the FIRST character (in a simple text field)? In other words, if the user accidentally hits the space key before typing anything else, it will be essentially be ignored. (I know there are ways to strip a leading space after the text is submitted, but I need it to not even get to that point for reasons I won't bore you with). Any help/ideas would be greatly appreciated. -Greg I'd like to hear these long and boring reasons that somehow justify client-side validation over server-side validation. Quote Link to comment https://forums.phpfreaks.com/topic/161290-prevent-a-space-from-being-first-characted-typed-in-a-text-field/#findComment-851127 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.