Using RegExp to make field validation easier

There is alot of ways to validate fields. But if you ask me validating thru regular expression is very powerful because you can test for patterns in strings. You can test for valid email adresses, phone numbers or your required passwords.

Because regular expressions can be tested both on the client and the server using almost the same code you can do tests on the client side without doing round trips to the server when the user exits a field or perhaps while the user are typing.

Lets look at an example of an regexp

[a-z]+

This will check that the tested string contains letters in the interval a-z

(\d{5})

The above code will test so the string is five numbers.

This is very helpful because then you could test for a password pattern

((?=.*\d)(?=.*[a-z])(?=.*[A-Z]))

This will then test the string to contain a digit, a lowercase and an uppercase letter. Imagine testing this some other way, no thanks. To complete this we also need to test for length and that we do by adding this to the line .{8,14} this will test that the string is 8 to 14 letters long. So the complete string is then

((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,14})

This seams complicated you might say, Yes regexp can be complicated but then we have great resources online to just copy and paste regexp from. But test the regexp you copy from the web, they might not do what you want them to do 😉

The best regexp tester that I have found is Rubular

How to use Regexp with old school Domino and XPages

How do you use this in Domino if you haven’t moved over to XPages yet, you could use this in client side javascript. And if you have moved over to XPages you can use this in Client side JS, Serverside JS and with a expression validator. I’m going to show you all these ways to validate.

1. Client side JS validation using RegExp

var field=document.getElementById("YOURFIELDNAME");
var data=field.value;
var re=/\d+/
if(re.test(data)){
   alert("Valid");
}else{
   alert("Not Valid");
}

2. Serverside javascript

The first Two lines changes from the client side script and also you need to write what should happen if the validation is true or not.

var inputText1= getComponent("inputText1");
var data=inputText1.getValueAsString()

var re=/\d+/
if(re.test(data)){
//do some stuff
}else{
//Do other stuff
}

3. validateExpression validator

The only thing diffrent in this validator than number 2 is that you return true or false when validation is done.

4. validateConstrain validator

This validator is used if you want to do a complete match of the value in the field using regular expression. My suggestion is that you start the string with ^( write you expression and end with )$ because then it’s easy to use a regexp tester when testing your regular expression this will tell the regexp engine that the string must validate as a line of text.

Update:

Check out this great post about how to creat regex strings directly in the designer.

http://www.intec.co.uk/test-your-regular-expressions-in-domino-designer/#comment-1498

Leave a Reply

1 Comment on "Using RegExp to make field validation easier"

Notify of
avatar
Sort by:   newest | oldest | most voted
Karl-Henry Martinsson
Guest

I blogged about how to use regexp in Lotusscript recently:
http://www.bleedyellow.com/blogs/texasswede/entry/regular_expressions_in_notes_lotusscript

So you can do it in Lotusscript, not just client side Javascript. 🙂

wpDiscuz