Reputation: 1567
I am making an form on my website but I have an problem with 2 regexs. (I have an PHP regex and an JS regex because I read that javascript only is dangerous)
First is in PHP
$name = mysql_real_escape_string($_POST['name']);
if(strlen($name) < 2 || strlen($name) > 20 || preg_match('#[0-9]#',$name))
{
echo '<p class="center"> je naam: <b> '.$name.' </b> bevat cijfers en/of mag niet korter zijn dan 2 tekens en niet langer zijn dan 20 tekens. </p>';
echo '<p class="center"> <a href="javascript:history.go(-1);">Ga terug</a></p> ';
}
this preg_match give's an error if there are numbers in the name. The question is I dont want that people have &^*#@ and that things in there name.
What is the correct regex for that.
Next is Javascript
function checkform ( form )
{
if (form.name.value == "")
{
alert( "Please enter your name." );
form.name.focus();
return false ;
}
if (form.email.value == "")
{
alert( "Please enter your email address." );
form.email.focus();
return false ;
}
return true ;
}
Here I want the same thing and validate that checks emptyness and length and no #$^&!
For email validate I need another regex but i search that one later.
Greeting Jochem
Please edit if you see mistakes.
EDIT:
var re and var regex are email regexs that i found. But why wont they work kan someone give me an good answer with explination because i am not so good. :)
function checkform ( form )
{
var rex = /[^a-z]/i;
var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (regex.test(form.email.value))
{
alert( "Please enter your email address." );
form.email.focus();
return false ;
}
if (rex.test(form.name.value))
{
alert( "Please enter your name. no numbers or #$^& things allowed)." );
form.name.focus();
return false ;
}
return true ;
}
Upvotes: 0
Views: 1652
Reputation: 324750
If you only want to accept letters, use /[^a-z]/i
. You can use this regex in PHP and in JavaScript.
Upvotes: 2