Verification of IP addresses and subnet masks

Source: Internet
Author: User

/**
* Function name: validatemask
* Function: verify the validity of the subnet mask.

* Function Author: 236f (fuwei236 # gmail.com)

* Input parameter: maskstr: The subnet mask in dotted decimal format (for example, 255.255.255.192)
* Main function:

* Call the function: _ checkiput_fomartip (IP)
* Return value: true: maskstr is a valid subnet mask.

* False: maskstr is an invalid subnet mask.
**/

Function validatemask (maskstr)

{
/* Validity verification */
VaR ippattern =/^/d {1, 3}/./d {1, 3}/./d {1, 3}/./d {1, 3} $/

If (! Ippattern. Test (maskstr) return false;

/* Check the Domain value */

VaR iparray = maskstr. Split (".");
VaR IP1 = parseint (iparray [0]);

VaR ip2 = parseint (iparray [1]);
VaR IP3 = parseint (iparray [2]);

VaR ip4 = parseint (iparray [3]);

If (IP1 <0 | IP1> 255/* The value range of each domain is 0-255 */

| Ip2 <0 | ip2> 255.
| IP3 <0 | IP3> 255

| Ip4 <0 | ip4> 255)
{
Return false;
}

/* Check whether the binary value is valid */
// Concatenate a binary string

VaR ip_binary = _ checkiput_fomartip (IP1) + _ checkiput_fomartip (ip2) + _ checkiput_fomartip (IP3) + _ checkiput_fomartip (ip4 );

If (-1! = Ip_binary.indexof ("01") return false;
Return true;
}

/**
* Function name: _ checkiput_fomartip
* Function: returns the 8-bit binary value corresponding to the input parameter.

* Function Author: 236f (fuwei236 # gmail.com)
* Input parameter: IP Address: the point-to-decimal value (0 ~ 255), int type value,

* Main function: validatemask
* Call a function: None

* Return value: the binary value corresponding to the IP address. For example, if 255 is input, 11111111 is returned. If 1 is input, 00000001 is returned)
**/

Function _ checkiput_fomartip (IP)
{

Return (IP + 256). tostring (2). substring (1); // format the output (add zero)
}

=============================== Separator ==========================

Test page test.htm

=============================== Separator ==========================
<HTML>

<Head>
<Title> </title>

<SCRIPT src = "validatemask. js"> </SCRIPT>

<SCRIPT>
Function Test (maskstr)
{

Alert ("IP Address:" + maskstr + "Verification Result:" + validatemask (maskstr ));
}

</SCRIPT>
</Head>
<Body>
<Center>

<H1> Subnet Mask Verification
</H1>

<Input type = "text" name = "Mask" id = "Mask" value = "">

<Input type = "button" name = "mytest" id = "mytest" value = "click to verify the value in the input box" onclick = "test (mask. Value)">

</Center>
</Body>
</Html>

 

 

 

====================

 

// Verify the IP address Validity
Function validateip (IP ){
VaR result = false;
If (! (/^ (/D {1, 3}) (/./d {1, 3}) {3} $/. Test (IP ))){
Return false;
}
VaR respacecheck =/^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]) /. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) /. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) /. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $ /;
VaR validip = IP. Match (respacecheck );
If (validip! = NULL ){
Result = true;
}
Return result;
}

// Verify the validity of the subnet mask
Function validatenetmast (netmask ){
VaR result = false;
If (! (/^ (/D {1, 3}) (/./d {1, 3}) {3} $/. Test (netmask ))){
Return true;
}
VaR respacecheck =/^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]) /. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) /. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) /. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $ /;
VaR validip = netmask. Match (respacecheck );
If (validip! = NULL ){
VaR mask_binary = conversionbinary (netmask );
If (-1! = Mask_binary.indexof ("01 ")){
Result = true;
}
} Else {
Result = true;
}
Return result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.