0

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 ;
    }
2
  • I would like to recommend you use a JavaScript framework like jQuery and use the jQuery validate plugin. This will give you great client-side validation that has been tested on all browsers, etc. Especially when I come to specific validation like e.g. email, URL, (dutch) postcode and also gives additional regex possibilies and server-side captcha validation, etc: tinyurl.com/2e9cgj And demos: tinyurl.com/6ad8o9 Commented Feb 11, 2012 at 20:54
  • Thank you. I will look this later, this is for a practice thingie. Commented Feb 11, 2012 at 20:58

1 Answer 1

2

If you only want to accept letters, use /[^a-z]/i. You can use this regex in PHP and in JavaScript.

Sign up to request clarification or add additional context in comments.

1 Comment

Your email regexes test if the email is valid, so you need to trigger the error when !regex.test().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.