0

I would like to validate user input client side with a little jQuery function that is called onsubmit on my form. I want the field #fname (first name) to only allow a-z, A-Z and space. The return false is supposed to be stopping the form from submitting.

function validateregister(){
    if (!($("fname") =~ [a-zA-Z ])) {
        return false;
    }
}

This is my HTML:

<input type="submit" value="Join now!" id="registersubmit" class="paddingoutline2" onsubmit="return validateregister()">

Of course, i'm going to validate the user input on the server side later on. When I submit the form, it gives me an "internal server error". This makes me think that I made an error in my function validateregister(). Is there anything wrong? If the I'm pretty new to jQuery so any help is appreciated.

Thanks!

1
  • 1
    The =~ is Perl syntax, not Javascript. Commented Mar 28, 2013 at 14:55

2 Answers 2

4

What you want is

function validateregister(){
    return /^[a-zA-Z ]+$/.test($('#fname').val());
}

Apart fixing the selector suggesting the use of the val and test functions, I took the liberty to change the regex :

  • ^ and $ force the test to cover the whole string
  • the + requires at least one character

But are you aware that this regex might be too strict if you want people to type their real first name ? Yours, for example, would not pass...

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

3 Comments

That's something I had not thought about, you're absolutely right i'm missing a lot of characters. Thanks for pointing that out! Is it normal that it returns false and that the webpage still submits the form? I would like it to stay on the same webpage and only show an error message.
onsubmit should be on the form element, not on the submit one.
De rien, ce fut un plaisir ;)
1

You have to use regex this way:

function validateregister(){
    var nameRgx = /[a-zA-Z]/;
    var phoneRgx = /[0-9]/;
    if (!nameRgx.test($("#fname").val())) {
       return false;
    }
    if (!phoneRgx.test($("#phone").val())) {
       return false;
    }
}

And make sure to refer your elements with Either with # id notation or . class notation. In your code you are not referencing your elem in a proper way.

^ ---->Start of a string. 
$ ---->End of a string. 
. ----> Any character (except \n newline) 
{...}----> Explicit quantifier notation. 
[...] ---->Explicit set of characters to match. 
(...) ---->Logical grouping of part of an expression. 
* ---->0 or more of previous expression. 
+ ---->1 or more of previous expression. 
? ---->0 or 1 of previous expression; 
       also forces minimal matching when an expression might 
       match several strings within a search string. 

More Info about Regex writing

Comments

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.