1

I am looking out for a regular expression that allows user to enter just alphabets and single space after one word or character before second word or character.

this is what I have done so far.

^[\w ]+$

but the checks only for word.

I require the above to validate name.

I want user to avoid typing characters like }{[]""<>?/\ and so on...

my jsfiddle it does not allow me thing

JSFiddle

  • 1
    Seems like a strange way to validate a name, why not just filter out special characters, as there are many weird names. – adeneo Aug 05 '13 at 10:55
  • I want user to avoid typing characters like }{[]""<>?/\ –  Aug 05 '13 at 10:56
  • Why can't I have quote marks in my name? I *want* a name with quote marks! – Spudley Aug 05 '13 at 11:23
  • yes I want that to @Spudley can you please suggest some thing even my jsfiddle does not seem to work –  Aug 05 '13 at 11:25
  • Your main problem is likely to be limitations in Javascript's regex parser, that doesn't properly support UTF-8. You'd be better off creating an exclusion regex pattern that blocks the characters you don't want, rather than an inclusive one like you have. – Spudley Aug 05 '13 at 11:33
  • @Spudley can you help me out with the regex. –  Aug 05 '13 at 11:38

6 Answers6

5

This simple one should suit your needs:

^\w+( \w+)*$

Regular expression visualization

Edit live on Debuggex

Community
  • 1
  • 1
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • I just want characters no nos no {}\? etc –  Aug 05 '13 at 11:57
  • /[^a-zA-Z ]/g i have got this can you guide me how can i allow user for just 1 space –  Aug 05 '13 at 12:07
  • @FakhruddinUjjainwala I don't get why you said. `\w` is exactly the same as `[a-zA-Z0-9_]`, i.e. one of these chars. If you need to add extra matches, for example dots and dashes, you can use `[\w.-]`. Special case for the dash: it should be either at the beginning or at the end of the class (i.e. `[\w.-]` rather than `[\w-.]`). – sp00m Aug 05 '13 at 13:03
  • OP doesn't want numbers or underscores (even though they had `\w` in their question). – nnnnnn Aug 05 '13 at 19:53
2

Your regex, ^[\w ]+$, allows one or more of any combination of word characters \w or spaces, included several spaces in a row or nothing but spaces. You don't want \w either, because it matches "word" characters where for some reason "word" has been defined as including digits and underscores.

Try the following instead:

^[A-Za-z']+( [A-Za-z']+)*$

This will match:

^                  beginning of string
[A-Za-z']+         one or more letters or apostrophe
( [A-Za-z']+)*    zero or more instances of (a single space followed by
                   by one or more letters or apostrophe)
$                  end of string

You didn't mention apostrophes, but I added it in to allow for names like O'Conner.

Note though that this excludes quite a few non-English names. Assuming you're validating people's names here I think you're better off just letting the user enter what they want to enter.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Hyphens are also common name characters. Also, [this answer](http://stackoverflow.com/a/15806080/838807) suggests a pattern for handling other unicode letters for non-English names – musefan Aug 05 '13 at 11:07
  • @musefan - Good point about the hyphens. Easy enough to add to the pattern I showed, or of course your link is good. But I stick by the last sentence of my answer, that it's not really particularly useful to even bother validating names. Let the user proof-read their own data for fields like name that don't need to be plugged into maths formulae or anything. – nnnnnn Aug 05 '13 at 11:10
  • I agree with you that the validation is over the top. I would probably just consider replacing any "multiple spaces" with a single space during the save process (or whatever is being done with the data) and trim the ends – musefan Aug 05 '13 at 11:12
  • @nnnnnn updated my jsfiddle please check if its correct as I am unable to type anything –  Aug 05 '13 at 11:21
  • You're using a keydown handler to test the field's current value, but the keydown handler is called before the field value is updated with the typed character. So it always fails. If you want as-the-user-types validation try using a keypress handler and test `event.which` to see which character they just typed. – nnnnnn Aug 05 '13 at 11:35
  • @nnnnnn I am sorry but I am totally confused can you please guide me with the code. I have got many different opinion for this question. Need to get the correct on working. –  Aug 05 '13 at 11:42
  • That worked for me only thing is can you tell me how to detect limit the single space after the word or disallow multiple spaces –  Aug 05 '13 at 16:26
  • That regex already limits the spaces to a single space between words. – nnnnnn Aug 05 '13 at 19:51
0

Your main problem is likely to be limitations in Javascript's regex parser, that doesn't properly support UTF-8. You'd be better off creating an exclusion regex pattern that blocks the characters you don't want, rather than an inclusive one like you have.

A pattern like this might work:

/^[^}{\[\]\"]+$/g

This will match any string that doesn't contain }{[]" characters. (add others as you require, but avoid matching too much).

Here's an example you can run in the browser console:

var x = '田中太郎';                    //sample name.
var filter_name = /^[^}{\[\]\"]+$/g;  //add more characters here to exclude as required.
console.log(filter_name.test(x));     //returns true (successful match).

var x = '田中{}太郎';                  //sample name with {} added.
var filter_name = /^[^}{\[\]\"]+$/g;  //add more characters here to exclude as required.
console.log(filter_name.test(x));     //returns false (failed due to {} braces).

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • how should I implement this in my jsfiddle so that whenever someone types any invalid characters the input is not counted atall. –  Aug 05 '13 at 11:48
  • just change the `filter_name` line in your code. I even used the same variable name as you for the regex. – Spudley Aug 05 '13 at 11:51
  • /[^a-zA-Z ]/g i have got this can you guide me how can i allow user for just 1 space –  Aug 05 '13 at 12:07
0

use this regex :

/^(\w+\s?)*$/
lashab
  • 186
  • 1
  • 7
0
/^([A-Za-z']+( [A-Za-z']+)*){3,30}$/
Anita Mourya
  • 115
  • 11
-1

Just add a space in your character class!

^[a-zA-Z0-9_ ]*$

Dinesh
  • 54
  • 5
  • That doesn't match a "single space", it permits multiple consecutive spaces or nothing but spaces, and a name isn't going to have digits in it (assuming OP is talking about a person's name). – nnnnnn Aug 05 '13 at 10:59
  • its not matching the criteria. I dont want nos in name. –  Aug 05 '13 at 11:03