jquery - HTML Form IP Address Validation -
i looking validate ip addresses on webpage using jquery. there 2 little updates help with.
how canvalid ip
, invalid ip
appear when page loads, not when text box edited. how can style text valid ip
appears in greenish , invalid ip
appears in red? i have created js fiddle of work far.
how can valid ip , invalid ip appear when page loads, not when text box edited.
you can wait until dom-tree loaded , fetch ip , perform validation:
$(document).on("ready", function() { var ip = getip(); // way implement it. validateipaddress(ip); });
where validateipaddress
method changes text according whether pattern valid or not:
if (!pattern.test(ip)) { $('#validate_ip').text('not valid ip'); } else { $('#validate_ip').text('valid ip'); }
how can style text valid ip appears in greenish , invalid ip appears in red? create css file contain classes like
.valid { color: #00ff00; /* greenish */ } .invalid { color: #ff0000; /* reddish */ }
then can add together classes jquery:
if (!pattern.test(ip)) { $('#validate_ip').text('not valid ip'); $('#validate_ip').addclass('valid'); } else { $('#validate_ip').text('valid ip'); $('#validate_ip').addclass('invalid'); }
jquery forms validation
No comments:
Post a Comment