0

I have an input box where the a username is input'd but if invalid characters are input'd, I want it to error. The code below is what I'm using; What would i put in the "something" section?

    var numbers = new RegExp("SOMETHING");

    $(this).removeClass("active");          

    if(($(this).val() == "") || $(this).val().match(numbers))
    {
        $("#firstNameErrorMsg").html("First name can only contain letters. ");
    }
    else
    {
        $("#firstNameErrorMsg").html("OK");
    }
smitelli
  • 6,835
  • 3
  • 31
  • 53
user1725794
  • 247
  • 1
  • 5
  • 14
  • 1
    That depends on your definition of "invalid characters" ;-) – Matthias Oct 06 '12 at 22:46
  • Well, I want it to only contain letters, so everything that's not a letter would be invalid. Hope this answers your question! – user1725794 Oct 06 '12 at 22:49
  • Are you only talking about Englisch letters? If not, you should take a look at http://stackoverflow.com/a/397801/232175 and rather explicitly check for invalid characters – Matthias Oct 06 '12 at 23:02

6 Answers6

2

Try this Regex

[A-Za-z]

This will match only lowercase and uppercase characters

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • The last two answers don't appear to work, or my code is wrong. When I test the field it doesn't throw the error or say OK, so it appears to be getting stuck at something. – user1725794 Oct 06 '12 at 23:03
2

Here are some patterns I wrote them long years ago:

patt['name'] = /^[a-z ,-]+$/i;
patt['username'] = /^[A-z0-9_-]+$/i;
patt['email'] = /^[a-z0-9]+(?:[\.-]?[a-z0-9]+)*@[a-z0-9]+([-]?[a-z0-9]+)*[\.-]?[a-z0-9]+([-]?[a-z0-9]+)*([\.-]?[a-z]{2,})*(\.[a-z]{2,5})+$/i;
patt['website'] = /^http(s)?:\/\/(www\.)?[a-z0-9]+([-]?[a-z0-9]+)*[\.-]?[a-z0-9]+([-]?[a-z0-9]+)*([\.-]?[a-z]{2,})*(\.[a-z]{2,5})+$/i;
patt['age'] = /^(?:([1][3-9]|[2-9][0-9]))$/i;
patt['subject'] = /[a-z0-9?!:;'&_\. ,-]+/i;

If you want to use them, you should check this condition:

if(($(this).val() == "") || ! $(this).val().match(patt['name'])){ // in case.
...
}

But if you want to check undesirable characters, it'll be a long pattern for username input.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • The last two answers don't appear to work, or my code is wrong. When I test the field it doesn't throw the error or say OK, so it appears to be getting stuck at something. – user1725794 Oct 06 '12 at 23:07
  • Got this method working without the "patt" section but I'm having issues combining it with the checking if the field is empty check as they both need different error messages. http://pastebin.com/CQGUL80r – user1725794 Oct 06 '12 at 23:15
  • take a look here: this is the [HTML part](http://pastebin.com/9bgg8s24), and this is the [JavaScript](http://pastebin.com/xZDpKCac). – Hashem Qolami Oct 07 '12 at 00:14
0

If you are looking for validating your users input , to only have letters , i would suggest using the char code, something like this :

  1. add the keypress event on the input tag

  2. for the event args passed, check the character code (Some browsers use keyCode, others use which)

    function checkOnKeyDown(event){
    if (event.KeyCode >= 65 && event.keyCode <=122) { //all ok here -- only upper/lowercase letters accepted } else { //wrong } }

Here is a list with all the keyCode to characters mapping ;) : http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

Alex Peta
  • 1,407
  • 1
  • 15
  • 26
0

Suggest you read a bit about regexes and experiment with them.

To get simply letters and nothing else, just do:

^[a-zA-Z]+$

That allows 1..n lowercase & uppercase letters to be found between start and end, nothing else. Sushanth's version will match partial pieces of the input, letting the user to use spaces, numbers, etc. elsewhere as long as there's one piece of of the input with a word in it.

t0mppa
  • 3,983
  • 5
  • 37
  • 48
0

This should be a full implementation of what you're trying to do:

var invalid = /[^A-Za-z]+/;

$(this).removeClass("active");          

if($(this).val() == "" || invalid.test($(this).val()))
{
    $("#firstNameErrorMsg").html("First name can only contain letters. ");
}
else
{
    $("#firstNameErrorMsg").html("OK");
}
smitelli
  • 6,835
  • 3
  • 31
  • 53
0

Sushanth is mostly correct, but you will need to match any number of letters, and it has to be from the start to the end only letters, so you should do something like this

var name = new RegExp('^[A-Za-z]+$');
$(this).removeClass('active');

if($(this).val().match(name)) {
    $('#firstNameErrorMsg').html('OK');
} else {
    $('#firstNameErrorMsg').html('First name can only contain letters.');
}