简体   繁体   English

HTML表单IP地址验证

[英]HTML Form IP Address Validation

I am looking to validate IP addresses on a webpage using jQuery. 我正在寻找使用jQuery验证网页上的IP地址。 There are two small updates I would like some help with. 我需要两个小更新。

  1. How can I get the Valid IP and Invalid IP to appear when the page loads, not just when the text box is edited. 如何使Valid IPInvalid IP在页面加载时出现,而不仅仅是在文本框被编辑时出现。
  2. how can I style the text so that Valid IP appears in green and Invalid IP appears in red? 如何设置文本的样式,以使“ Valid IP显示为绿色,而“ Invalid IP显示为红色?

I have created a JS Fiddle of my work so far . 到目前为止,我已经完成了我的工作

  1. How can I get the Valid IP and Invalid IP to appear when the page loads, not just when the text box is edited. 如何使有效IP和无效IP在页面加载时出现,而不仅仅是在文本框被编辑时出现。

You can wait until the dom-tree is loaded and then fetch the ip and perform the validation: 您可以等到dom-tree加载后再获取ip并执行验证:

$(document).on("ready", function()
{
    var ip = getIp(); // The way you implement it.
    validateIpAddress(ip);
});

Where your validateIpAddress method changes the text according to whether the pattern is valid or not: 您的validateIpAddress方法根据模式是否有效来更改文本的位置:

if (!pattern.test(ip))
{
    $('#validate_ip').text('Not Valid IP');
}
else
{
    $('#validate_ip').text('Valid IP');
}
  1. how can I style the text so that Valid IP appears in green and Invalid IP appears in red? 如何设置文本的样式,以使“有效IP”显示为绿色,而“无效IP”显示为红色?

Create a CSS file which contain classes like 创建一个CSS文件,其中包含类似的类

.valid
{
    color: #00ff00; /* green */
}

.invalid
{
    color: #ff0000; /* red */
}

Then you can simply add the classes with jQuery: 然后,您可以使用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');
}

For question 2: 对于问题2:

Create a class valid and a class invalid, add the class with jquery by doing this: 创建一个有效的类和一个无效的类,通过执行以下操作用jquery添加该类:

$('.sn').addClass('valid');

$('.sn').addClass('invalid');

in the style define: 在样式中定义:

.valid{
  border:thin solid green;
}

.invalid{
  border:thin solid red;
}

I hope this can help. 希望对您有所帮助。

I modified your fiddle 我修改了你的小提琴

http://jsfiddle.net/r5aeus62/9/ http://jsfiddle.net/r5aeus62/9/

$(document).ready(function() {
  var pattern = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
  x = 46;

  var ip = $('.ip').val();
  var sn = $('.sn').val();
  var br = $('.br').val();
  var nt = $('.nt').val();
  var gw = $('.gw').val();

  if (!pattern.test(ip)) {
     $('#validate_ip').text('Invalid IP');
     $('#validate_ip').addClass('invalid');
  } else {
     $('#validate_ip').text('Valid IP');
     $('#validate_ip').removeClass().addClass('valid');
  }

  if (!pattern.test(sn)) {
     $('#validate_sn').text('Invalid IP');
     $('#validate_sn').removeClass().addClass('invalid');
  } else {
     $('#validate_sn').text('Valid IP');
     $('#validate_sn').removeClass().addClass('valid');
  }

  if (!pattern.test(br)) {
     $('#validate_br').text('Invalid IP');
     $('#validate_br').addClass('invalid');
  } else {
     $('#validate_br').text('Valid IP');
     $('#validate_br').removeClass().addClass('valid');
  }

  if (!pattern.test(nt)) {
     $('#validate_nt').text('Invalid IP');
     $('#validate_ip').removeClass().addClass('invalid');
  } else {
     $('#validate_nt').text('Valid IP');
     $('#validate_nt').removeClass().addClass('valid');
  }

  if (!pattern.test(gw)) {
     $('#validate_gw').text('Invalid IP');
     $('#validate_gw').removeClass().addClass('invalid');
  } else {
     $('#validate_gw').text('Valid IP');
     $('#validate_gw').removeClass().addClass('valid');
}

Just look on the fiddle for the whole code. 只需在小提琴上查看整个代码即可。

Happy Coding :D 快乐编码:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM