Validations on variable text field - Allow only Numbers and Special Characters

KARAN24
Tera Contributor

Hi All,

I want a validation  in my variable - only numbers and special characters should be allowed

Below code did not work. Can someone please help with the code for all the validations.

var pattern = /^[_A-z]*((-|\s)*[_A-z])*$/;

var name = g_form.getValue('number');

if (!pattern.test(number)){

g_form.addErrorMessage("Only Numbers and Characters are allowed");

}

}

 Thanks,

Karan

8 REPLIES 8

Trung
Kilo Guru

could you try with the code below?

var pattern = /^[0-9*#+]+$/;

var value = g_form.getValue('number');

if (!pattern.test(value)) {

    g_form.addErrorMessage("Only Numbers and Characters are allowed");
    g_form.clearValue("value")

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.

 

Madhuri Jagdeo
Tera Contributor

Hi KARAN, 

Please try below code in your instance. As I have tried it in my own instance and its working perfectly fine. 

function onSubmit() {
var pattern = /^[0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/;
var value = g_form.getValue('u_string_1');

if (!value.match(pattern)) {
g_form.addErrorMessage("Only Numbers and Characters are allowed");
// g_form.clearValue("value");
return false;

}
}

I have applied this regex on bookname field and records are inserted as below:find_real_file.png

And giving error as per requirement of no letters:

find_real_file.png

 

Mark Correct if this solves your issue!!

Please mark this answer Helpful if you find this helpful.

Regards,

Madhuri Jagdeo

Rekha Esadkar
Tera Expert

Adding to @madhuri

onSubmit() You also first check for Field is blank or not.

var value = g_form.getValue('u_string');

if (value == " ") {
    alert("Name must be filled out/ cannot blank");
    return false;
  }

 

Please mark this answer Helpful if you find this helpful.

Rameshwar Khile
Mega Expert

 

Hi @karan

 You can try this code. it is working in my PDI.

 

function onSubmit() {
   //Type appropriate comment here, and begin script below
 var pattern = /^[0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/;
var value = g_form.getValue('u_lname');

if (!value.match(pattern)) {
g_form.addErrorMessage("Only Numbers and Characters are allowed");
return false;


}  
}

 

 

 

 

 

 

Mark  Correct if this solves your issue!!

Please mark this answer Helpful if you find this helpful.

Regards,

Rameshwar