正则表达式检查IP和EMAIL字符串的格式(Javascript实现)

80酷酷网    80kuku.com

  javascript|正则|字符串

function.js

//check the format of IP address
//Write by Ken
function checkIP(ip)
{
 var re = /^((\d)|(([1-9])\d)|(1\d\d)|(2(([0-4]\d)|5([0-5]))))\.((\d)|(([1-9])\d)|(1\d\d)|(2(([0-4]\d)|5([0-5]))))\.((\d)|(([1-9])\d)|(1\d\d)|(2(([0-4]\d)|5([0-5]))))\.((\d)|(([1-9])\d)|(1\d\d)|(2(([0-4]\d)|5([0-5]))))$/;
 
 if(re.test(ip))
  return true;
 else 
  return false;

}

//check the format of email
//Write by Ken
//Note:
//Begining by number or leter.
function checkEmail(email)
{
 var re = /^((\d|[a-z]|[A-Z])(((\d|[a-z]|[A-Z]|\_){1,19})))((((\d|[a-z]|[A-Z]){1,10})\.){1,4})(((\d|[a-z]|[A-Z])){2,10})$/;
 //var re = /^((\d|[a-z]|[A-Z])|(((\d|[a-z]|[A-Z]){1,19})))((((\d|[a-z]|[A-Z])\.){1, 4})(((\d|[a-z]|[A-Z])){2, 10}))$/;
 
 if(re.test(email))
  return true;
 else
  return false;
}

function onIPClick()
{
 if(checkIP(form1.ip.value))
  alert('true');
 else
  alert('false');
}

function onEmailClick()
{
 if(checkEmail(form1.email.value))
  alert('true');
 else
  alert('false');
}

 

test.html

<html>
 <head>
  <script language='javascript' src='function.js'>
  </script>
 </head>
 <body>
  <form name='form1'>
   <span>
    <input type='text' id='ip' name='ip' value='127.0.0.1'></input>
    <input type='button' value='IP' ></input>
   </span>
  </form>
 </body>
</html>


 



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: