所以我想知道如果以下是合法的,也许它不工作,由于语法错误。简单验证单个搜索字段上的四条规则。感谢您对优雅和启发性解决方案的任何帮助!javascript jquery里面的语句准备好了吗?
$(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');
}
}
});
});
当您尝试运行它究竟会发生什么?如果您想停止提交,您需要从提交处理程序返回false。 – nnnnnn
如果你看了这个问题,它清楚地表明它不会因为语法错误而运行。 –
@Matthew Flaschen - 不,它说“可能因为语法错误而无法工作?”,“不工作”几乎可能意味着任何事情,包括跑步,但给出错误的结果。 (并且我确实指出了我认为最可能发生的问题。) – nnnnnn