Так что мне интересно, допустимо ли следующее, возможно, оно не работает из-за синтаксической ошибки.?Простая проверка четырех правил в одном поле поиска.Спасибо за любую помощь в поиске элегантного и информативного решения!
$(function() {
$('input.text').focus(function() {
$(this).removeClass('noSubmitX');
$('.enterX').removeClass('now').text( orig);
}); //reset error status
var orig = "Enter the title you want.";
var msg1 = "Title must be between 3 and 63 characters.";
var msg2 = "Title cannot begin or end with a hypen";
var msg3 = "Title cannot contain a hyphen at character positions 3 and 4";
$('form.xSearch').submit(function() {
var theSearch = $('input.text').val();
var xLong = $('input.text').val().length;
var firstx = (theSearch[0]);
var thirdx = (theSearch[2]);
var fourthx = (theSearch[3]);
var lastx = (theSearch[xLong - 1]);
try {
if (xLong < 2 || xLong > 62) {
throw "msg1";
}
else if (firstx == "-") || (lastx == "-") {
throw "msg2";
}
else if (thirdx == "-") && (fourthx == "-")
{
throw "msg3";
}
}
catch (er) {
if (er == 'msg1') {
$('input.text').addClass('noSubmitX');
$('.enterX').addClass('now').text('Title must be between 3 and 63 characters.');
}
if (er == 'msg2') {
$('input.text').addClass('noSubmitX');
$('.enterX').addClass('now').text('Title cannot begin or end with a hypen');
}
if (er == 'msg3') {
$('input.text').addClass('noSubmitX');
$('.enterX').addClass('now').text('Title cannot contain a hyphen at character positions 3 and 4');
}
}
});
});