John
John

Reputation: 4944

Making Form Input Data Lower-case Letters

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 tabindex="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.

Upvotes: 2

Views: 6100

Answers (4)

anoraq
anoraq

Reputation: 515

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.

Upvotes: 0

corey
corey

Reputation: 41

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();">

Upvotes: 4

maid450
maid450

Reputation: 7486

in the form processing (asuming you are using GET method) do:

$username = strtolower($_GET['username'])

Upvotes: 4

Sarfraz
Sarfraz

Reputation: 382696

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']);

Upvotes: 1

Related Questions