Thursday, January 8, 2015

Peoplecode Regular Expression Checks for Valid Phone Number, Email Address, or URL

In this article are snippets of code that allow me to check to see if the data input into a field matches a phone number format, and email address format, or a web site URL format.

Phone Number Validation.

I placed the following into the component FieldEdit event of the MY_RECORD.MY_PHONE field.

Local JavaObject &text;
Local JavaObject &expression;
Local JavaObject &converter;

&text = CreateJavaObject("java.lang.String", MY_RECORD.MY_PHONE.Value);

&expression = GetJavaClass("java.util.regex.Pattern").compile
("^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$"); 
              /* phone number format (999) 999-9999 */
&converter = &expression.matcher(&text);
If &converter.find() Then
   MessageBox(0, "", 0, 0, "matches pattern");
Else
   MessageBox(0, "", 0, 0, "not matches pattern");
   &expression = GetJavaClass("java.util.regex.Pattern").compile("^\d{10}$"); 
              /* 10 digits */
   &converter = &expression.matcher(&text);
   If &converter.find() Then
      MessageBox(0, "", 0, 0, "matches digits pattern");
   Else
      MessageBox(0, "", 0, 0, "not matches digits pattern");
   End-If;
End-If;

1. Create a &text Java object to hold the value of the record field on your page.  
2. Create an &expression Java object to hold the compiled regular expression pattern that you want to match against.
3. Create a &converter Java object and use it to perform a match between the &expression pattern and the &text value.
4. Perform the find() method on the &converter object to see if the &expression pattern matched the &text value or not.

In the above code snippet, I don't mind if the user typed the phone number as 10 digits or in (999) 999-9999 format, so I check for both.

Email Address Validation.

Here's a function that uses a regular expression to validate Email Addresses.

Function validateEmailAddress(&inEmailAddress As string)
   Local JavaObject &text;
   Local JavaObject &expression;
   Local JavaObject &converter;
   
   &text = CreateJavaObject("java.lang.String", &inEmailAddress);
   rem MessageBox(0, "", 0, 0, "validating");
   &expression = GetJavaClass("java.util.regex.Pattern").compile("^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"); /* email address format someone@somewhere.domain */
   &converter = &expression.matcher(&text);
   If &converter.find() Then
   Else
      Error MsgGetExplainText(99999, 1, "Error: The Email Address %1 is invalid.", &inEmailAddress);
   End-If;
End-Function;

Phone Number Validation.

Here's a function that uses a regular expression to validate a URL.

Function validateURL(&inURL As string)
   Local JavaObject &text;
   Local JavaObject &expression;
   Local JavaObject &converter;
   
   &text = CreateJavaObject("java.lang.String", &inURL);
   rem MessageBox(0, "", 0, 0, "validating");
   &expression = GetJavaClass("java.util.regex.Pattern").compile("^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$"); /* URL format something.somewhere.domain */
   &converter = &expression.matcher(&text);
   If &converter.find() Then
   Else
      Error MsgGetExplainText(99999, 1, "Error: The URL %1 is invalid.", &inURL);
   End-If;
End-Function;

Warning: When working with Java objects, don't include WinMessage statements.  If you do, you'll get errors about Java objects not being serializable.  Use MessageBox statements instead.

4 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

Hi, to see if you can help me with an Regular Expression.

I try to make one but I can not until now.

The format that I need to validate for the phone field is as follows: number phone correct is 4121234567 or 4141234567 or 4241234567 or 4261234567 or 4161234567.

1) can not start with cero (0)
2) Only accept numeric characters.
3) the first three digits must be 412, 414, 424, 426, 416.
4) The remaining 7 numbers should be numbers only.

It is possible to do this in a Regular Expression in PeopleSoft ?

Thanks for your answer.

Logan L Watson said...

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. Modern Behaviour Pty Ltd