Need client script for user should enter value up to 50characters long. No special characters(@,#)

User_267
Tera Contributor

Require client script for user should enter the value up to 50 characters long.No special characters(@,#,*), if not following alert appears:

"You entered special character-please renter"

2 REPLIES 2

Saurabh Gupta
Kilo Patron

Hi,
You can use below regex-

var regex = /^[^@#*]{1,50}$/;

var input = "YourStrin@gHere"; // Replace with your input string

 

if (regex.test(input)) {

    gs.log("Valid string");

} else {

    gs.log("Invalid string");

}

Thanks and Regards,

Saurabh Gupta

praneeth7
Tera Guru

Hi @User_267 

Use this script in onSubmit CS

 

 

function onSubmit() {
  var field = g_form.getValue('field_name'); // Replace 'field_name' with the actual field name

  // Define a regular expression to match special characters
  var specialCharsRegex = /[@#*]/;

  if (specialCharsRegex.test(field)) {
    alert('Special characters (@, #, *) are not allowed. Please reenter.');
    return false;
  }

  // Check if the field value is longer than 50 characters
  if (field.length > 50) {
    alert('Value should be up to 50 characters long. Please reenter.');
    return false; 
  }

  return true;
}

 

 

If you find this useful Please Mark my answer as Helpful and Accepted solution

Thank you

Praneeth S