This is the code I wrote to accept input only in the form xay
(there is p
instead of a
)
jQuery(function(){
jQuery(".search-submit").click(function(){
jQuery(".error").hide();
var hasError = false;
var searchReg = /^[p0-9]+$/;
var searchVal = jQuery(".search-field").val();
var searchLen = searchVal.length;
var num1 = ['1','2','3','4','5','6','7','8','9'];
var num0 = ['0','1','2','3','4','5','6','7','8','9'];
if(searchLen < 3) {
jQuery(".search-field").after('<span class="error">Enter at least 3 characters.</span>');
hasError = true;
} else if(searchLen > 7) {
jQuery(".search-field").after('<span class="error">Enter no more than 7 characters.</span>');
hasError = true;
} else if(num1.indexOf(searchVal.charAt(0)) == -1) {
jQuery(".search-field").after('<span class="error">First character has to be a digit between 1 and 9.</span>');
hasError = true;
} else if(num0.indexOf(searchVal.charAt(searchLen-1)) == -1) {
jQuery(".search-field").after('<span class="error">Last character has to be a digit between 0 and 9.</span>');
hasError = true;
} else if(!searchVal.includes('p')) {
jQuery(".search-field").after('<span class="error">You have to enter the character "p".</span>');
hasError = true;
} else if((searchVal.match(/p/g) || []).length > 1) {
jQuery(".search-field").after('<span class="error">Enter only 1 character "p".</span>');
hasError = true;
} else if(!searchReg.test(searchVal)) {
jQuery(".search-field").after('<span class="error">Enter only digits and one "p" character.</span>');
hasError = true;
} else if(searchVal.split('p')[0].length > 3 || searchVal.split('p')[1].length > 3) {
jQuery(".search-field").after('<span class="error">Exercise and page have to be numbers between 1 and 999.</span>');
hasError = true;
} else if(searchVal.charAt(searchVal.indexOf('p')+1) == '0') {
jQuery(".search-field").after('<span class="error">Page first digit cannot be 0.</span>');
hasError = true;
}
if(hasError == true) {return false;}
});
});