Wordpress Themes - Medios de comunicación - Debt Consolidation - Labortechnik Magnetrührer Kalorimeter - Flash Games

PDA

View Full Version : Form Validation


oo7ml
Jun 18th 2007, 5:48 pm
I have some form validation done, but it's not very good. How do i make sure that users can only use a-z, A-Z, 0-9. (there for users would not be able to use special characters) - note: i have the php server side validation done but i want javascript also, thanks

Here is the code i have so far:

function validate_username(field,alerttxt)
{
with (field)
{
numofchar=value.length;
if (numofchar<5)
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{

if (validate_username(username,"Ooops... Username must be at least 5 characters in length")==false)
{username.focus();return false}


how can i make sure that the $username only can have a-z, A-Z, 0-9

krt
Jun 18th 2007, 10:07 pm
if (numofchar<5 || value.match(/[^a-z0-9]/i))

Also, you might want to update the message so it informs of this new check.

oo7ml
Jun 19th 2007, 5:45 am
How can i change this line around so that

if (numofchar<5 || value.match(/[^a-z0-9]/i))

The rules are:
between 5 and 15 characters
a-z A-Z 0-9 characters only

thanks for your help

nico_swd
Jun 19th 2007, 5:46 am
It's the same pattern as I gave you here.

http://forums.digitalpoint.com/showthread.php?t=370927


if (!value.match(/^[a-z0-9]{5,15}$/i))
{
// Error
}

oo7ml
Jun 19th 2007, 5:50 am
Sorry, i'm new to all of this, i didn't think you could use the same format in Javascript as you could in PHP. Thanks a million nico_swd, your a big help, thanks

nico_swd
Jun 19th 2007, 5:52 am
No problem. :D

oo7ml
Jun 19th 2007, 6:23 am
Sorry, i'm after running into a bit of a problem. If i use

if (!value.match(/^[a-z0-9]{5,15}$/i))

If a users name is O'Neill it wont accept it becaue of the ' character in the name.

How do i just change the if statement to only check if its {5,15} or is their a way that i can inlude certain character such as ' and , and -

krt
Jun 19th 2007, 7:02 am
Add them to the list inside [ and ]. Usually it will work except in a few cases. e.g. a hyphen has to be escaped or at the end of the list: if (!value.match(/^[a-z0-9',-]{5,15}$/i))