1

Hi i have a field in php that will be validated in javascript using i.e for emails

var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

What i'm after is a validation check which will look for the first letter as a capital Q then the next letters can be numbers only then followed by a . then two numbers only and then an optional letter i.e Q100.11 or Q100.11a

I must admit i look at the above email validation check and i have no clue how it works but it does ;)

many thanks for any help on this

Steve

2
  • are you looking for the regular expression to use, or the javascript, or both? Commented Mar 19, 2011 at 20:50
  • im after the regex for the match Commented Mar 19, 2011 at 20:51

3 Answers 3

2

The ^ marks the beginning of the string, $ matches the end of the string. In other words, the whole string should exactly match this regular expression.

  • [\w-\.]+: I think you wanted to match letters, digits, dots and - only. In that case, the - should be escaped (\-): [\w\-\.]+. The plus-sign makes is match one or more times.
  • @: a literal @ match
  • ([\w-]+\.)+ letters, digits and - are allowed one or more times, with a dot after it (between the parentheses). This may occur several times (at least once).
  • [\w-]{2,4}: this should match the TLD, like com, net or org. Because a TLD can only contain letters, it should be replaced by [a-z]{2,4}. This means: lowercase letters may occur two till four times. Note that the TLD can be longer than 4 characters.

An regular expression which should follow the next rules:

  • a capital Q (Q)
  • followed by one or more occurrences of digits (\d+)
  • a literal dot (.)
  • two digits (\d{2})
  • one optional letter ([a-z]?)

Result:

var regex = /Q\d+\.\d{2}[a-z]?/;

If you need to match strings case-insensitive, add the i (case-insensitive) modifier:

var regex = /Q\d+\.\d{2}[a-z]?/i;

Validating a string using a regexp can be done in several ways, one of them:

if (regex.test(str)) {
   // success
} else {
   // no match
}
Sign up to request clarification or add additional context in comments.

Comments

1
var emailRegex = /^Q\d+\.\d{2}[a-zA-Z]?@([\w-]+\.)+[a-zA-Z]+$/;
var str = "[email protected]";
alert(emailRegex.test(str));

2 Comments

I'm guessing you might also include a sample of how to use it in JS.
Close but not quite. It's not * but ? right before the @. Also there was no case given on the last letter, so better make it both. Lastly, 4 letters are not enought for TLDs (museum comes to mind) but they are letters only. /^Q\d+\.\d{2}[a-zA-Z]?@([\w-]+\.)+[a-zA-Z]+$/
-1

var regex = /^Q[0-9]+\.[0-9]{2}[a-z]?$/;

+ means one or more

the period must be escaped - \.

[0-9]{2} means 2 digits, same as \d{2}

[a-z]? means 0 or 1 letter

You can check your regex at http://regexpal.com/

3 Comments

Your dot will match any symbol, not just a dot. It should be a \. instead. Also, [a-z] does not include uppercase letters.
yeh, I see you need to code the line for the \ to show. Thanks for the downvote.
Hi thanks, the reg code actually only allows one letter at the end which i was after ;) thanks foson

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.