Highlighted
Absent Member.
Absent Member.
10172 views

Validating Name field on Custom form


I'm trying to validate Name field on a custom form using following regex but running into an issue.

(!str.match(/^[a-z]([-']?[a-z]+)*( [a-z]([-']?[a-z]+)*)+$/))

Script is added within HTML/JavaScript Widget. Issue I'm having is to do with " ' " in this line. It seems that " ' " is taken as a comment rather than part of the regex and therefore not even getting evaluated,

I've checked my script in another tool for validity and code seems to be right but it's not working when I try to include it in the widget.

e.g., Name = O'Donnel and O*Donnel. I want to validate that 1st name is correct name entry but 2nd entry is wrong.
Tags (3)
0 Likes
7 Replies
Highlighted
Not applicable

Re: Validating Name field on Custom form


have you tried using the \ before the ' to indicate that ' is not a comment
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Validating Name field on Custom form


Thank you for quick reply. I've tried adding " \ " but that didn't work.

Also, tried following:

1) "'"

2) \n

Regards,
0 Likes
Not applicable

Re: Validating Name field on Custom form


not sure then, perhaps someone else will chime in
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Validating Name field on Custom form


That regex will only match lower case letters and it does expect a string in the form:

somethingSPACEsomething, the SPACE being required.

I'm having trouble pasting a suggested regex string into GetSatisfaction. See this Demo on JSFiddle.
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Validating Name field on Custom form


I looked at the code at JSFiddle. It seems to work fine over there. But even if I take the exact code and add to HTML/Javascript widget; I'm getting an error.

I'm including the script here;

///////////script///////////////

function correctFormat() \{

var str = GetFieldValue("Employee Name");

var str_rgx = /^[-,\. a-z'`]+$/i;

var checkedName;

alert("str is " + str);

alert("regex is " + str_rgx);

if (str.match(str_rgx)) \{

alert ( '|' + str + '|=Match \n' );

checkedName= str;

} else \{

alert ( '|' + str + '|=no match \n');

checkedName= '';

}

alert("This is the str " + formatted);

SetFieldValue("Employee Name", formatted);

}

AddChangeCallback("Employee Name", correctFormat);

//////////////////script//////////////////

In the following line after the " ' " sign (comment sign) SBM Composer turns the rest of statement into a comment.

var str_rgx = /^[-,\. a-z'`]+$/i;

Thank you for your help.
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Validating Name field on Custom form


Try not using the HTMLJavascript widget or Form Actions.

Put the code into a .js file, import it into a JavaScript entity in Composer then associate the Javascript with the Form.
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Validating Name field on Custom form


Thank you Paul, that worked. (I think above mentioned code could have worked as widget or Form Action. It was only me who was not paying attention. I'll test that later).Once again, thank you for your help.P.S: (Note to future self). ' or " is different than //.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.