...

View Full Version : php validation(no spaces and special chr's)



john_zakaria
01-21-2010, 02:23 PM
please i want to know how to make a validation using php in the way that only characters(A-Z, a-z) , number and _ are allowed to be used

special characters and spaces are not allowed (will return error message)


can anyone help me please

kbluhm
01-21-2010, 02:28 PM
if ( preg_match( '/\W/', $input ) )
{
// illegal characters exist
}

// or...
if ( preg_match( '/^\w+$/', $input ) )
{
// all characters are legal
}

john_zakaria
01-21-2010, 02:37 PM
if ( preg_match( '/\W/', $input ) )
{
// illegal characters exist
}

// or...
if ( preg_match( '/^\w+$/', $input ) )
{
// all characters are legal
}


Sorry i cant understand what did you mean.. could you please explain more?

kbluhm
01-21-2010, 02:46 PM
Yeah, still early in the day here.

\w matches any word character, which includes (tada!) letters, numbers, and underscores only.

\W does just the opposite, and matches anything that is not a word character.

\w+ matches 1 or more consecutive word characters, ^ signifies the start of the subject or line, and $ signifies the end.

So ^\w+$ will only match a subject that is made up of 1 or more word characters.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum