Thank you so much, TME, for getting back to me. So each of the four input fields calls its own validation function upon onblur.
1. first name
My.validateFirstName = function(id, value) {
console.log('name: id: ' + id + ' value: ' + value);
// Validation rule
var re = /^([ \u0430-\u044f\u00c0-\u01ffa-zђјљњћџA-Z'\-])+$/;
// Check input
if (re.test(value)){
// Style green
document.getElementById(id).style.background ='#ccffcc';
// Hide error prompt
document.getElementById(id + 'Error').style.display = "none";
State.variables.userFirstname = value;
State.variables.firstNameOK = true;
return true;
} else {
// Style red
document.getElementById(id).style.background ='#e35152';
// Show error prompt
document.getElementById(id + 'Error').style.display = "block";
return false;
}
};
2.surname
My.validateSurname = function(id, value) {
console.log('name: id: ' + id + ' value: ' + value);
// Validation rule
var re = /^([ \u0430-\u044f\u00c0-\u01ffa-zђјљњћџA-Z'\-])+$/;
// Check input
if (re.test(value)){
// Style green
document.getElementById(id).style.background ='#ccffcc';
// Hide error prompt
document.getElementById(id + 'Error').style.display = "none";
State.variables.userSurname = value;
State.variables.surnameOK = true;
return true;
} else {
// Style red
document.getElementById(id).style.background ='#e35152';
// Show error prompt
document.getElementById(id + 'Error').style.display = "block";
return false;
}
};
As you can see, these two only differ in setting either State.variables.firstNameOK or State.variables.surnameOK to true in case validation was successful. They also set the respective input value to a SugarCube variable.
3. Email
My.validateEmail = function(id, value) {
console.log('email: id: ' + id + ' value: ' + value);
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (re.test(value)) {
document.getElementById(id).style.background ='#ccffcc';
document.getElementById(id+'Error').style.display = "none";
State.variables.userMail = value;
State.variables.mailOK = true;
return true;
} else {
document.getElementById(id).style.background ='#e35152';
document.getElementById(id + 'Error').style.display = "block";
return false;
}
};
4. Phone number
My.validateNumber = function(id, value) {
console.log('name: id: ' + id + ' value: ' + value);
// Validation rule
var re = /^[0-9]+$/;
// Check input
if (re.test(value)){
// Style green
document.getElementById(id).style.background ='#ccffcc';
// Hide error prompt
document.getElementById(id + 'Error').style.display = "none";
State.variables.userPhone = value;
State.variables.phoneOK = true;
return true;
} else {
// Style red
document.getElementById(id).style.background ='#e35152';
// Show error prompt
document.getElementById(id + 'Error').style.display = "block";
return false;
}
};
Input field validation works as expected, what is causing me issues is the code within the <<button>> macro ath the bottom, which is expected to prevent from going to the next passage unless all four input fields have been adequately filled in.
<<button "Start!">>
<<if $firstNameOK eq false>>
<<replace "#answer-error">>Please enter a valid first name!<</replace>>
<<elseif $surnameOK eq false>>
<<replace "#answer-error">>Please enter a valid surname!<</replace>>
<<elseif $phoneOK eq false>>
<<replace "#answer-error">>Please enter a valid first phone number!<</replace>>
<<elseif $mailOK eq false>>
<<replace "#answer-error">>Please enter a valid first email-adress!<</replace>>
<<else>>
<<goto "next">>
<</if>>
<</button>> <span id="answer-error"></span>
It seems to determine phoneOK, firstNameOK and surnameOK to be TRUE, which they shouldn't be. As for your comment on anchoring the regex on both ends, I have to admit I do not understand what you mean.
Best regards,
richVIE