Regular expressions and script includes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2013 02:06 PM
I have a regular expression to validate/format phone numbers and I can get it to work in a client script. when I try it in a script include it fails on line 11, but no error message. It's not going to the else branch, I had a log statement there and no output. I'ts like there's some catch block that traps the error before it gets to line 16. e Any ideas? I tried using "new RexExp" and that didn't seem to help either.
Thanks,
Frank
1 var ValidatePhoneNum = Class.create(); 2 3 ValidatePhoneNum.prototype = Object.extendsObject(AbstractAjaxProcessor, { 4 5 validatePhoneNum: function() { 6 var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; 7 var phoneNum = this.getParameter('sysparm_phone_num'); 8 var answer = false; 9 10 if (regexObj.test(phoneNum)) { 11 answer = phoneNum.replace(regexObj, "($1) $2-$3"); 12 } else { 13 // Invalid phone number 14 } 15 16 return answer; 17 }, 18 19 _privateFunction: function() { // this function is not client callable 20 } 21 });
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2013 03:10 PM
I wouldn't know a regular expression from a Pop-Tart, and this may not help you, but I did extract a bit of your script and successfully run it in Scripts - Background. The following gave the output "*** Script: (123) 123-1235"
var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
var phoneNum = "123-123-1235";
var answer = false;
if (regexObj.test(phoneNum))
answer = phoneNum.replace(regexObj, "($1) $2-$3");
else
gs.print("dude");
gs.print(answer);
And when I set phoneNum = "123-123-12345" in the script, I got the following:
*** Script: dude
*** Script: false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2013 07:03 AM
Thanks, I too was able to get it to work in Scripts - Background, but getting it to work in a script include, is still not working. I just might have to submit a ticket, it should work in all forms of scripts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2013 01:37 PM
Replace this line
var phoneNum = this.getParameter('sysparm_phone_num');
with
var phoneNum = this.getParameter('sysparm_phone_num') + ''; //to convert it to string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2013 08:12 AM
Thanks, that helped. Heres' all the changes I made to get it to work:
6 var regexObj = new RegExp('\^\\\(\?\(\[0\-9\]\{3\}\)\\\)\?\[\-\. \]\?\(\[0\-9\]\{3\}\)\[\-\. \]\?\(\[0\-9\]\{4\}\)\$');
7 var phoneNum = this.getParameter('sysparm_phone_num') + '';
8 var answer = 0;
Line 6 - I needed to escape all of the special characters and remove the forward slashes that enclosed the expression.
Line 7 - Converted phoneNum to a string.
Line 8 - When an invalid phone number was entered, I had issue with returning false, so I just used zero instead.
It would be nice if we were given access to the internal type of ph_number, it's on the user record. It looks like it uses some validation checks. If you enter 1234567890, it won't format it, but if you enter 5551115555 it will format it to (555) 111-5555. Anyway, I got it working so I'm closing the issue.
Thanks again,
Frank