Validations on variable text field - Allow only Numbers and Special Characters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2021 06:35 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2021 07:34 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2021 08:59 PM
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:
And giving error as per requirement of no letters:
Mark ✅ Correct if this solves your issue!!
Please mark this answer Helpful if you find this helpful.
Regards,
Madhuri Jagdeo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2021 09:37 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2021 09:52 PM
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