Expand my Community achievements bar.

Validation suggestion

Avatar

Level 7

How I can validate the pattern below:

4digits dash 3 digits dash 3 digits

for example: 1234-567-892

Thank you

6 Replies

Avatar

Level 2

Use a regular expression in the validate event of the field.

var fieldRegExp = "^[0-9]{4}-[0-9]{3}-[0-9]{3}$"; // nnnn-nnn-nnn

if (this.rawValue.search(fieldRegExp) === -1) {
     false;
}
else {
     true;
}

Avatar

Level 7

Hi Jason, thank you for your help again!

Can you explain to me what your script below means?

Just I like to learn a little more than copy your code......

if (this.rawValue.search(fieldRegExp) === -1)

Thanks again Jason

Avatar

Level 2

The first line of code is:

var fieldRegExp = "^[0-9]{4}-[0-9]{3}-[0-9]{3}$"; // nnnn-nnn-nnn


This is a regular expression to describe a pattern of characters: nnnn-nnn-nnn
Where n is a digit from 0 to 9.

Components of the regular expression:
^     indicates the start of the string
[]    contains a range of characters, in this case 0 to 9, any of which are valid

{}    contains a number that specifies the expected number of the previous item

-     is literal, i.e. you are expecting a dash between the groups of numbers

$     indicates the end of the string

The next bit of code is:

if (this.rawValue.search(fieldRegExp) === -1) {
     false;
}
else {
     true;
}


"search" is a method of the JavaScript String object. The search method searches for a match between a regular expression (in this case: fieldRegExp) and a string (in this case: this.rawValue). The method returns the position of the match or -1 if no match is found.

If the regular expression pattern is contained in this.rawValue, the search method will match it and return its position in the string. In this case, the position would always be 0 because we use ^ to specify that the pattern must be at the start of the string.

If the pattern is not found in this.rawValue then -1 will be returned.

There are plenty of regular expression and JavaScript resources on the web, just do a search. Here's one example for the "search" method: http://www.w3schools.com/jsref/jsref_search.asp

Avatar

Level 7

Hi Jason,

was nice of you to take of your time and explain to me in details about how the code works. Thank you!

One more question and I promise you is the last one!

We like in this patern to add a addition at the end.

The addition could be two uppercase letters or one uppercase or none.

For example: 1234-123-123-AB or 1234-123-123-A  OR 1234-123-123

I came up with this code below but......does not work!

var fieldRegExp = "^[0-9]{4}-[0-9]{3}-[0-9]{3}-[A-Z]{2}||[A-Z]{1}||[A-Z]{0}$";// nnnn-nnn-nnn

{false;}

else

{true; }

How we can approch this matter?

Thanks for all your time

Avatar

Level 2

Try this regular expression:

"^[0-9]{4}-[0-9]{3}-[0-9]{3}(-[A-Z]{1,2})?$";

The addition is: (-[A-Z]{1,2})?

[A-Z]     Can be any character in the range of A-Z (case-sensitive)

{1,2}     Means that the preceding item (the A-Z) is expected to occur 1 or 2 times

()?        These brackets group this part of the pattern and the question mark says that the group can occur 0 or 1 times

Avatar

Level 7

THANK YOU Jason for all your help!

Have a happy holidays!