Validating names in textboxes in PHP, HTML or Javascript
When I type in a name in a textbox on a form, how can I validate that name before the user clicks submit?
I see this all the time in CMS and in s开发者_如何转开发ome forums and never been able to figure it out.
Joe
That depends entirely on what you want to validate. Are you trying to filter out potentially dangerous characters? Is there a format you want names to adhere to?
If you want to filter it before the user clicks submit, then you'll need to use Javascript.
Read in the value of the textbox, either as the user types it, or after focus is no longer on the textbox, and then inspect the value to see if you're happy with what has been put in. The easiest way to do that is by using regular expressions. Look them up.
You can disable the submit button until the text field validates.
However, it may still be possible to submit the form, so make sure you do validation on the server side too. Again, it depends entirely on what you want to do - PHP has lots of built-in functions for validating strings to filter out malicious characters, HTML entities, and more.
Figure out what you want to do, then look it up in a search engine.
I am assuming you are talking about seeing if a username is available or if the username is valid - the best way to do this is by using a jQuery plugin.
There are plenty of tutorials available online to walk you though the all steps.
Such as jQuery Username Availability check.
精彩评论