多くの条件ステートメントを使用し、複雑になったコードがあります。入力を検証するためのより効率的な方法を探しています。
私のコードは、サーバー側の検証の結果を 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 には、3 つの入力要素 (住所、市区町村、市区町村) とそれに付随するラベルが含まれています。