最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add a validation error message next to a field using jQuery - Stack Overflow

programmeradmin4浏览0评论

Hi have a form with a few fields. Amongst them:

<div>
    <label for="phoneNumber" class="label">Phone Number</label>
    <input name="phoneNumber" type="text" id="phoneNumber" size="13"  style="float:left;margin-right:10px;">
</div>
<div>
    <input type="checkbox" name="activePN" id="activePN" checked >
    <label for="activePN">Active</label>
</div>

The, when the form is submited, I want to validate the input and write next to each field for whichever field didn't validate. Like this:

$('#submit').click(function () {
    var proceed = true;
    var strippedPN = $('#phoneNumber').val().replace(/[^\d\.]/g, '').toString(); //strips non-digits from the string
    if (strippedPN.length !== 10) {
        $('#phoneNumber').text('<p>Phone number has to be 10 digits long.</p>')
        proceed = false;
    }
...
...
...
});

I was hopping that adding those <p> </p> tags would do it. But they don't... Note: I also tried with html() instead of text() and with activePN instead of phoneNumber.

Hi have a form with a few fields. Amongst them:

<div>
    <label for="phoneNumber" class="label">Phone Number</label>
    <input name="phoneNumber" type="text" id="phoneNumber" size="13"  style="float:left;margin-right:10px;">
</div>
<div>
    <input type="checkbox" name="activePN" id="activePN" checked >
    <label for="activePN">Active</label>
</div>

The, when the form is submited, I want to validate the input and write next to each field for whichever field didn't validate. Like this:

$('#submit').click(function () {
    var proceed = true;
    var strippedPN = $('#phoneNumber').val().replace(/[^\d\.]/g, '').toString(); //strips non-digits from the string
    if (strippedPN.length !== 10) {
        $('#phoneNumber').text('<p>Phone number has to be 10 digits long.</p>')
        proceed = false;
    }
...
...
...
});

I was hopping that adding those <p> </p> tags would do it. But they don't... Note: I also tried with html() instead of text() and with activePN instead of phoneNumber.

Share Improve this question asked Aug 15, 2013 at 23:08 AmarundoAmarundo 2,39716 gold badges52 silver badges70 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

Use .after().

$('#phoneNumber').after('<p>Phone number has to be 10 digits long.</p>')

It might be wise to add a class to your p tag too, so you can remove them when the number is edited to be correct.

Try:

$('#submit').click(function(){
  var proceed = true;
  var strippedPN = $('#phoneNumber').val().replace(/[^\d\.]/g, ''); //strips non-digits from the string - already a String
  if(strippedPN.length !== 10){
    $('#phoneNumber').after('<p>Phone number has to be 10 digits long.</p>')
     proceed = false;
  }
}

Its best to use jqueryvalidation plugin.

But in some scenario may be you need to show validation message using custom code, then below may help.

Code

var errorSeen = false;

$('#txtname').keyup(function (e) {

var validInput = false; // TODO set your validation here

if (!validInput) {

  var errorMessageVisible = $(".validationMessage").is(":visible");

  if (errorSeen === false && errorMessageVisible === false) {

      $('#txtname').style.borderColor = "red";

        $('#txtname').after("<span class='validationMessage' style='color:red;'>
                            Name is required.</span>");

        errorSeen = true;
    }


  }
   else {

   $('#txtname').style.borderColor = "";

     var errorMessageVisible = $(".validationMessage").is(":visible");

     if (errorMessageVisible)
        $(".validationMessage").remove();

        errorSeen = false;
  }

});
发布评论

评论列表(0)

  1. 暂无评论