Hi all,
I am working on a web application where I need to apply field validation on several inputs on the first page of the survey, I do believe it can be done via initialization script and approval script tabs.
Here are the fields that I am looking to validate:
A - Should not allow future dates
B - Must be an @specificdomain email
C - Exactly 8 alphanumeric characters (no prefixes like #)
D- Limited to 4 characters
E - Must follow a valid telephone number format
F - Introduce a strict validation that only allows the email.
*The letters are used to reference the variables.
Any guidance would be highly appreciated.
Best,
Celia
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hello @ccg1706
It is doable using the approval script.
Here is an example:
i've created a webApp with field A to F as bellow, each field value is stored in variables fieldA to fieldF:
i gave each input an alias so that i can point to it when showing the error message :
and in the approval script i used the script bellow :
function checkEmail(email, domain) {
var result = {
isEmailValid: false,
isEmailInDomain: false
};
// Basic email format validation
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
result.isEmailValid = emailRegex.test(email);
// Check if the email ends with the specified domain (case-insensitive)
if (result.isEmailValid) {
var emailDomain = email.split('@')[1].toLowerCase();
var expectedDomain = domain.toLowerCase().replace(/^@/, '');
result.isEmailInDomain = emailDomain === expectedDomain;
}
return result;
}
function isValidSpanishPhone(phone) {
var regex = /^(?:\+34|0034)?[6789]\d{8}$/;
return regex.test(phone.replace(/\s+/g, ""));
}
//Validate field A
var dateFieldA = parseTimeStamp(ctx.vars.fieldA);
var dateNow = new Date();
if ( dateFieldA == undefined || dateFieldA > dateNow ){
serverForm.logInputError("fieldA","field A : Future dates not allowed","fieldA");
}
//validate field B
if ( ctx.vars.fieldB == undefined || !(checkEmail(ctx.vars.fieldB, "@gmail.com").isEmailValid && checkEmail(ctx.vars.fieldB, "@gmail.com").isEmailInDomain) ){
serverForm.logInputError("fieldB","field B : Email must be an @gmail email","fieldB");
}
//validate field C
var regex8Char = /^[A-Za-z0-9]{8}$/;
if ( ctx.vars.fieldC == undefined || !regex8Char.test(ctx.vars.fieldC) ){
serverForm.logInputError("fieldC","field C : this field should be exactly 8 alphanumeric characters (no prefixes like #)","fieldC");
}
//validate field D
if ( ctx.vars.fieldD == undefined || ctx.vars.fieldD.toString().length > 4 ){
serverForm.logInputError("fieldD","field D : this field is limited to 4 characters)","fieldD");
}
//Validate field E
if ( ctx.vars.fieldE == undefined || !isValidSpanishPhone(ctx.vars.fieldE)){
serverForm.logInputError("fieldE","field E : Must follow a valid telephone number format","fieldE");
}
//validate field F
if ( ctx.vars.fieldF == undefined || !(checkEmail(ctx.vars.fieldF, "").isEmailValid) ){
serverForm.logInputError("fieldF","field F : Please enter a valid email","fieldF");
}
Result :
result when all checks are ok (except the check on the date so that i can stay on this page):
i've packaged the webApp (attached) if you want to test it on your end.
Br,
Hi @Amine_Abedour,
Thanks for sharing, I have tested it on my sandbox and it works all correctly. When I take it to customer variables it throws the following error, that I attach you a screenshot.
My alias variables are the following ones:
-dateOfVisit
-accountManagerEmail
-contractNumber
-premiseNumber
-contactPhone
-invoiceEmail
And the code is the one that you gave me but replacing with the previos alias.
function checkEmail(email, domain) {
var result = {
isEmailValid: false,
isEmailInDomain: false
};
// Basic email format validation
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
result.isEmailValid = emailRegex.test(email);
// Check if the email ends with the specified domain (case-insensitive)
if (result.isEmailValid) {
var emailDomain = email.split('@')[1].toLowerCase();
var expectedDomain = domain.toLowerCase().replace(/^@/, '');
result.isEmailInDomain = emailDomain === expectedDomain;
}
return result;
}
function isValidSpanishPhone(phone) {
var regex = /^(?:\+34|0034)?[6789]\d{8}$/;
return regex.test(phone.replace(/\s+/g, ""));
}
//Validate dateOfVisit
var dateFieldA = parseTimeStamp(ctx.vars.dateOfVisit);
var dateNow = new Date();
if ( dateFieldA == undefined || dateFieldA > dateNow ){
serverForm.logInputError("dateOfVisit","Date of Visit: Future dates not allowed","dateOfVisit");
}
//validate accountManagerEmail
if ( ctx.vars.accountManagerEmail == undefined || !(checkEmail(ctx.vars.accountManagerEmail, "@rentokil.com").isEmailValid && checkEmail(ctx.vars.accountManagerEmail, "@rentokil.com").isEmailInDomain) ){
serverForm.logInputError("accountManagerEmail","Account Manager Email : Email must be a @rentokil email","accountManagerEmail");
}
//validate contractNumber
var regex8Char = /^[A-Za-z0-9]{8}$/;
if ( ctx.vars.contractNumber == undefined || !regex8Char.test(ctx.vars.contractNumber) ){
serverForm.logInputError("contractNumber","Contract Number : this field should be exactly 8 alphanumeric characters (no prefixes like #)","contractNumber");
}
//validate premiseNumber
if ( ctx.vars.premiseNumber == undefined || ctx.vars.fieldD.toString().length > 4 ){
serverForm.logInputError("premiseNumber","Premise Number : this field is limited to 4 characters)","premiseNumber");
}
//Validate contactPhone
if ( ctx.vars.contactPhone == undefined || !isValidSpanishPhone(ctx.vars.contactPhone)){
serverForm.logInputError("contactPhone","Contact Phone : Must follow a valid telephone number format","contactPhone");
}
//validate invoiceEmail
if ( ctx.vars.invoiceEmail == undefined || !(checkEmail(ctx.vars.invoiceEmail, "").isEmailValid) ){
serverForm.logInputError("invoiceEmail","Invoice Email : Please enter a valid email","invoiceEmail");
}
By the error it throws I do believe is a comma missing but I don't know where.
Thanks again for your help.
Best,
Celia
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies