Prevent double quotes and 'enter' key in mutliline field

LG21
Giga Contributor

We need to prevent users from entering double quotes and the "enter" key (ie a new line) into a comments field which is multiline.

We've created an on change client script, but the alert displays even if there is no double quote or new line in the field. 

We've tried to use an example from another post but can't seem to get the correct values in the regEx var?

Our script is:

//check for double quotes or new line/enter
var str = g_form.getValue('u_comments');
var regEx = /"\n\r/;
var valid = regEx.test(str);
if(!valid)
alert('The comments field must not contain double quotes or Enter/New Line');

return false;
}

 

the alternative option is to replace double quotes and 'enter' with a space when the form is submitted - so happy to have that option as well.

 

thanks

1 ACCEPTED SOLUTION

If you want to do this in a client script you could use JavaScript indexOf() to determine if a quote exists. Something like this untested code:

var comment = g_form.getValue('u_comment');

if (comment.indexOf('"') >= 0) {
    alert('Hey, there is a quote here. Not good');
}

Footnote on the dictionary attribute, it caps the string limit at 255 characters, but for a comment, that's still a lot.

View solution in original post

8 REPLIES 8

johnfeist
Mega Sage
Mega Sage

Hi LG2,

While I'm no expert on this, it looks like the problem is with the statement if(!valid).  The test function will return true if any of the values in the mask are found. Here's a link to the online documentation.

Hope that helps.

:{)

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Chuck Tomasi
Tera Patron

An easy way to prevent enter is to add the dictionary attribute is_multi_text=false to the field (assuming you're talking about a field and not a variable.) This will force the field to display as a single line entry. FWIW, I haven't tried this on Journal fields so your mileage may vary.

Quotes then becomes a simple JavaScript replace().

Thanks Chuck, the attribute seems to work fine.  But (and please forgive the ignorance, it is Friday)  would the replace be on a business rule on submit/insert?

I did try that before but coudln't get that to work, hence I thought the alert might be easier, how wrong I was...

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

    var str = current.u_comments;
    str.replace ('"'," ");
})(current, previous);

 

 

If you want to do this in a client script you could use JavaScript indexOf() to determine if a quote exists. Something like this untested code:

var comment = g_form.getValue('u_comment');

if (comment.indexOf('"') >= 0) {
    alert('Hey, there is a quote here. Not good');
}

Footnote on the dictionary attribute, it caps the string limit at 255 characters, but for a comment, that's still a lot.