我的代码使用了许多条件语句并且变得复杂了。我正在寻找一种更有效的方法来验证输入。
我的代码以 JSON 形式接收服务器端验证的结果。对于许多输入中的每一个,如果输入的值无效,false
则服务器会返回对应的值。条件语句适当地修改 UI。这是一个示例:
else if((data.address===false)&&(data.city===false)&&(data.municipality===false))
{
$('#adrserror').html('You have to put an address, city and municiplaity.');
$('label.error').hide();
$('.openpaddress').find('input').prop('readonly', false);
}
else if((data.address===false)&&(data.city===false))
{
$('#adrserror').html('You have to fill the address and the city fields.');
$('label.error').hide();
$('.openpaddress').find('#municipality').prop('readonly', true).css('border', '0px');
}
else if((data.city===false)&&(data.municipality===false))
...
HTML 仅包含三个带有标签的输入元素(地址、城市和自治市)。