Using a Regular Expression check in a Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2011 01:20 PM
Hi -- I need to validate data entered in a field is in a proper ethernet address format ( 6 pairs of hex digits (0-F) separated by ":". After viewing other community posts, I come upon regex and though I could use it to alert the user is the input is not in the proper format. The problem I am having is telling regex to throw that error. The client script run on change of the "ethernet" filed. Any ideas on how I can modify this script to issues an alert? Here's what I'm using for a client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
var mac = g_form.getValue('ethernet');
alert(mac);
function getMACWithRegEx(mac) {
var parser = /[0-9A-F][0-9A-F]:{5}[0-9A-F][0-9A-F]/;
var ans = parser.exec(mac);
return (ans == null) ? null : ans[0];
}
}
Thanks in advance for any help.
j
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2011 06:33 PM
You can use a match function
var ipArray =mac.match(parser);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2011 08:04 AM
The parser.exec is returning null -- I guess I am misunderstanding what it should be returning. I expected it to match the field format to the format specified in the parser variable. If the value matches the format, I expected to see the ans variable with a true value; if it didn't match i expected to see a false. When running this script "ans" and "ipArray" are coming back as null when I use a good ethernet (01:02:03:04:05:06) and I get the same results when I use a bad ethernet (01:02:03:04:05:xx). Can you tell me what the parser.exec should return?
var mac = '01:02:03:04:05:xx'; // g_form.getValue('ethernet')
var result = getMACWithRegex(mac);
gs.log("Mac is: " + mac);
gs.log("Result is: " + result);
function getMACWithRegex(mac) {
var parser = /[0-9A-F][0-9A-F]:{5}[0-9A-F][0-9A-F]/;
var ipArray =mac.match(parser);
var ans = parser.exec(mac);
gs.log("ans is: " + ans);
gs.log("ipArray is: " + ipArray);
gs.log("parser is: " + parser);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2011 08:28 AM
Use a function like this. Note the regex is slightly different that what you have.
function isValidMAC(mac) {
var regex = /([0-9A-F][0-9A-F]:){5}([0-9A-F][0-9A-F]){1}/i;
return mac.search(regex) > -1;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2011 08:44 AM
Thank you Mike that was just what I needed! I appreciate the help.
Jack