javascript - How to add a validation error message next to a field using jQuery -
hi have form 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 form submited, want validate input , write next each field whichever field didn't validate. this:
$('#submit').click(function () { var proceed = true; var strippedpn = $('#phonenumber').val().replace(/[^\d\.]/g, '').tostring(); //strips non-digits string if (strippedpn.length !== 10) { $('#phonenumber').text('<p>phone number has 10 digits long.</p>') proceed = false; } ... ... ... });
i hopping adding <p> </p>
tags it. don't... note: tried html()
instead of text()
, activepn
instead of phonenumber
.
use .after()
.
$('#phonenumber').after('<p>phone number has 10 digits long.</p>')
it might wise add class p
tag too, can remove them when number edited correct.
Comments
Post a Comment