Validation Scripts – RegExr

Here are a selection of RegExr validation scripts to use with error messages.

Correct Email format
 
Checks to see if a email address has been formatted correctly
if(!IsEmail(f(CurrentForm())))
{
  RaiseError();
  SetQuestionErrorMessage(9,”Please provide a valid email address.”);
}
Correct Telephone format
Checks a open question has 11 digits without spaces, i.e Phone number
var regexPattern = /^\d{11}$/;
var qnTxt : String = f(CurrentForm()).get();
if(!regexPattern.test(qnTxt))
{
RaiseError();
    SetQuestionErrorMessage(9,”Must be 11 numbers with no space”);
}
Check referrence is in correct format – NN/NNNNN
var regexPattern = /^\d{2}\/\d{5}$/;
var qnTxt : String = f(CurrentForm()).get();if(!regexPattern.test(qnTxt))
{
RaiseError();
SetQuestionErrorMessage(2057,”The reference must be 8 characters in length in the format nn/nnnnn”);
}

If using this validation for a open text list the masking would look like

var regexPattern = /^\d{2}\/\d{5}$/;
var qnTxt : String = f(CurrentForm())[1,2,3,4,5,6,7,8,9,10].get();
if(!regexPattern.test(qnTxt))
{
    RaiseError();
    SetQuestionErrorMessage(2057,”The reference must be 8 characters in length in the format nn/nnnnn”);
}

Check for a Name 

Will not allow string to start with any character other than alphanumeric. Will allow – and ‘

var regexPattern = /^[a-zA-Z]([a-zA-Z-‘])+$/
var qnTxt : String = f(CurrentForm()).get();if(!regexPattern.test(qnTxt))
{
RaiseError();
SetQuestionErrorMessage(2057,”The field must be in the correct name format”);
}

 

Check for only letters, no spaces or special characters

var regexPattern = /^[a-zA-Z]([a-zA-Z])+$/
var qnTxt : String = f(CurrentForm()).get();

if(!regexPattern.test(qnTxt))
{
RaiseError();
SetQuestionErrorMessage(2057,”The field must be in the correct name format”);
}

Check for less than 2 digits in length AKA less than 100% for a open string field

var regexPattern = /^\d{2}$/;;
var qnTxt : String = f(‘qnr_detailsfrompurchaser_100_2_other’).get();
if(!regexPattern.test(qnTxt))
{
RaiseError();
SetQuestionErrorMessage(2057,”Must be less than 100, not including %”);
}