0

バリデーション付きのフォームを作成しています。たとえば、次のような入力フィールドと送信ボタンがあります。

<input name="Zip" maxlength="5" id="Zip" value="" />
<a class="form-submit submit-btn" href="javascript:void(0);" style="display: block;">Proceed</a>

jQueryを使用して、入力フィールドが空で送信しようとした場合、入力フィールド内の赤いテキストの新しい値を「Zip is Required」にしたい。

ユーザーがフィールドをクリックして編集すると、赤いエラー テキストが消え、郵便番号を入力できるようになります。

エラーメッセージが表示されている場合にのみ、入力フィールドの onclick をクリアしたいのです。これは理にかなっていますか?

これは私がこれまでに持っているものであり、jQuery は非常に新しいものです。クリックするとエラー メッセージは消えますが、新しい zip を入力してクリック アウトし、再度クリックすると、新しい zip は消えます。

if($('#Zip').val() == ''){
    $('#Zip').val('Zip is Required');
    $('#Zip').css('color', 'red');
    if($('#Zip').val() == 'Zip is Required'){  
        $('#Zip').click(function() {
            $('#Zip').val('');
            $('#Zip').css('color', 'black');
        });
    }
}

ご協力ありがとうございました!

4

2 に答える 2

1
 $(document).ready(function() {
        ///this section clears any text typed inside the box if the user decides to type again
       $('#Zip').click(function()
      {
          if(isNaN(CheckZip))
          {
            $('#Zip').val('');
           }
      });

///This section does the security checking to see if the input is empty and add the required text
$('.submit-btn').click(function()
      {
          var CheckZip = $('#Zip').val();
          if(!CheckZip)
          {
            $('#Zip').val('Zip is Required');
          }
      });
      ///Let me know if I`m missing anything
于 2013-09-13T19:50:17.790 に答える