0

I'm trying to write a regular expression that says first letter not to be uppercase and the rest 0-19 characters mixed case. This doesn't seem to do it.

!/^[A-Z][a-zA-Z]{0,19}$/ 
3
  • put a ^ inside your first [ ] or do [a-z] Commented Nov 3, 2012 at 20:15
  • You want lower-case then mixed-case? Commented Nov 3, 2012 at 20:17
  • He posted this as a comment to my question further down below: "I tried that it didn't help but I think there might be something besides my regx that's messing up my validation my function function validate_forename(field) { if (field === "") { return "No Forename was entered.\n"; } else if (!/^[A-Z][a-zA-Z]{0,19}$/.test(field)) { return "forename must begin with capital and be between 1 and 20 letters" } return ""; }" I can't help him, so I thought I'd bring it further up. Commented Nov 3, 2012 at 21:25

4 Answers 4

2

If you want the first letter lowercase, all the others lower-or-upper, you can do this:

/^[a-z][a-zA-Z]{0,19}$/ 

Notice that you cannot just say [^A-Z] because that would allow non-alpha characters through, like numbers.

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

Comments

1

One of many solutions is regex pattern

/^(?![A-Z])[a-zA-Z]{1,20}$/ 

...which reads: one to twenty letters with no uppercase in first place

Comments

0

Change from:

!/^[A-Z][a-zA-Z]{0,19}$/ 

To:

/^[^A-Z][a-zA-Z]{0,19}$/ 

That should fix your problem.

2 Comments

I tried that it didn't help but I think there might be something besides my regx that's messing up my validation my function function validate_forename(field) { if (field === "") { return "No Forename was entered.\n"; } else if (!/^[A-Z][a-zA-Z]{0,19}$/.test(field)) { return "forename must begin with capital and be between 1 and 20 letters" } return ""; }
Hmm. I'm not entirely sure what it could be the. Copy what you just wrote and put it as an edit to your question, so more people can see and help you out. Sorry.
0

Use [^A-Z] instead of [A-Z]

[^ ] is the opposite of [].It matches characters not contained within the brackests

So,it should be

/^[^A-Z][a-zA-Z]{0,19}$/ 

OR

simply use

 /^[a-z][a-zA-Z]{0,19}$/ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.