複数のテキストフィールドを検証する必要があるが正しいとは思えない関数があります。たとえば、$("#textfield").blur(validation);
を使用して、各フィールドがフォーカスを失ったときに検証関数を実行します(3つのフィールドすべてがblur
を使用して検証関数を呼び出します)。私はif
ステートメントを正しく注文していないと思う。また、this
を使用して検証を行う簡単な方法がわかっている場合は、それも素晴らしいでしょう。ありがとうございました。jsの1つの関数で複数のフィールドを検証する方法
function validation(height,width,color){
if (height.length == 0 || height == null){ //check for empty field
$("#msg").html("This field cannot be empty");
}
else if (isNaN(height)){ //check that height is a number
$("#msg").html("The height must be a number");
}else{
$("#msg").html("The height is " + height); //if height is ok shows this message
}
if (width.length == 0 || width== null){ //same for width
$("#msg").html("This field cannot be empty");
}
else if (isNaN(width)){
$("#msg").html("The width must be a number");
}else{
$("#msg").html("The width is " + width);
}
if (color.length == 0 || color == null){
$("#msg").html("This field cannot be empty");
}
else if (color == "white" || color == "grey"){ //check that color is one of these two options
$("#msg").html("The color is " + color);
}else{
$("#msg").html("The color must be white or grey");
}
}
この作業には単にブートストラップライブラリを使用できます – Saini
HTML5フォームの検証は役に立ちますか?スクリプトを使用する必要はありません。 [こちら](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation)を試してください。 – Doug
@Doug問題は私がテストのために勉強していることです。私はこれをこのようにする必要があります。 – Gonzalo