The code below is part of a form. I would like the "username" input below to be converted to all lower-case letters regardless of what the user enters. How could I do this?
<div class="usernameformfield"><input tab开发者_Python百科index="1" accesskey="u" name="username" type="text" maxlength="35" id="username" /></div>
EDIT: I know about strtolower. What I don't know is where to put it to make it work with the form.
in the form processing (asuming you are using GET method) do:
$username = strtolower($_GET['username'])
For a more instant conversion, the following code converts anything in an input box into lowercase, once the user selects anything else (the next box of a table for example)...
<input type="text" name="username" onChange="javascript:this.value=this.value.toLowerCase();">
You can use strtolower function for that.
Example:
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str;
Output:
mary had a little lamb and she loved it so
In your case, you would do:
echo strtolower($_POST['username']);
As most already have mentioned:
$username = strtolower($_POST['username']);
...would be the backend-solution. But to also provide somewhat the same for the user filling out the field a stylesheet (css) such as:
#username { text-transform: lowercase; }
...would be nice as well. Then they don't have to think twice about it, in case their mobile phone happens to have capitalized the first letter or something like that. This will then be javascript-less which is always to prefer, IMHO.
I would also have put on a trim() on the username backend wise as well.
精彩评论