www.pudn.com > Firewall_PNE_3_3.zip > validate.js, change:2009-03-16,size:15710b


/* validate.js - validate input fields for firewall web screens */

/* Copyright 2004 - 2005 Wind River Systems, Inc. */

function fillV4Addr()
{
form=document.forms['EditForm'];

//alert("fill v4 addr!");

if(form.fwWebSrcAddrStartDisplay.value == '' || form.fwWebSrcAddrStartDisplay.value == '::' )
    form.fwWebSrcAddrStartDisplay.value = '0.0.0.0';

if(form.fwWebSrcAddrEndDisplay.value == '' || form.fwWebSrcAddrEndDisplay.value == '::' )
    form.fwWebSrcAddrEndDisplay.value = '0.0.0.0';

if(form.fwWebDstAddrStartDisplay.value == '' || form.fwWebDstAddrStartDisplay.value == '::' )
    form.fwWebDstAddrStartDisplay.value = '0.0.0.0';

if(form.fwWebDstAddrEndDisplay.value == '' || form.fwWebDstAddrEndDisplay.value == '::' )
    form.fwWebDstAddrEndDisplay.value = '0.0.0.0';

return true;
}

function fillV6Addr()
{
form=document.forms['EditForm'];

//alert("fill v6 addr!");

if(form.fwWebSrcAddrStartDisplay.value == '' || form.fwWebSrcAddrStartDisplay.value == '0.0.0.0' )
    form.fwWebSrcAddrStartDisplay.value = '::';

if(form.fwWebSrcAddrEndDisplay.value == '' || form.fwWebSrcAddrEndDisplay.value == '0.0.0.0' )
    form.fwWebSrcAddrEndDisplay.value = '::';

if(form.fwWebDstAddrStartDisplay.value == '' || form.fwWebDstAddrStartDisplay.value == '0.0.0.0' )
    form.fwWebDstAddrStartDisplay.value = '::';

if(form.fwWebDstAddrEndDisplay.value == '' || form.fwWebDstAddrEndDisplay.value == '0.0.0.0' )
    form.fwWebDstAddrEndDisplay.value = '::';

return true;
}


function validateName(element) 
{
// create a regular expression to match the chars NOT allowed 
// in rule name
badChars = new RegExp('[^a-zA-Z0-9 _.]');

// test 
if (badChars.test(element.value)) 
    {
    // matched bad chars
    alert('Bad chars in rule name!');
    return false;
    }

return true;
}

function checkAddr(element)
{
form=document.forms['EditForm'];

//alert("check addr!");

if (form.fwWebDirection[2].checked || form.fwWebDirection[3].checked)
    {
//    alert ("check v6 addr");
    return(checkAddrV6(element));
    }
else
    {
//    alert("check v4 addr!");
    return (checkAddrV4(element));
    }
}

function checkAddrV6(element)
{
//alert("address being checked");
if( !CheckIPv6(element.value) )
    {
    alert("Invalid IPv6 address!");
    return false;
    }
return true;
}

function checkAddrV4(element)
{
//alert("address being checked");
if( !CheckIPv4(element.value) )
    {
    alert("Invalid IPv4 address! Please enter between 0.0.0.0 to 255.255.255.255.");
    return false;
    }
return true;
}

function isInteger(s)
{
var i;
for (i = 0; i < s.length; i++)
    {   
    // Check that current character is number.
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) 
        return false;
    }

// All characters are numbers.
return true;
}


function inRange(value, low, high)
{
// if (value<low) alert(value+"less than"+low);
// if (value>high) alert(value+"grater than"+high);

return( !(value<low)  &&  value<=high  );
}

//check setup.html on submit
function checkSetupForm(form)
{
//nothing to be done at this time
return true;
}

// return the selection of radio. If none, return -1
function requireRadios(form, radio, errString)
{   
var oneSelected = false;
var radioSelection = -1;

//alert("radio function called!" );

if (form[radio][0])
    {
    //alert("radio function called!" + radio + " length:" + form[radio].length +" name " + form[radio][0].name);

    for (var i=0;i<form[radio].length;i++)
        {
        if (form[radio][i].checked)
            {
            oneSelected = true;
            radioSelection = i;
            //alert("select"+ form[radio][0].name + i);
            return radioSelection;
            }
        //else alert("not select"+ form[radio][0].name + i);
        }     
    }
else
    {
    //alert("radio function called!" + radio + " length:" + form[radio].length +" name " + form[radio].name);
    if(form[radio].checked)
        {
        oneSelected = true;
        radioSelection = 0;
        //alert("select"+ form[radio].name + radioSelection);
        return radioSelection;
        }
    }

if(! oneSelected)
    {
    alert("Please select one option for " + errString + "!");
    return -1;
    }
}


function disableDays()
{
frm=document.forms['EditForm'];

frm.fwWebSunday.disabled=true;
frm.fwWebMonday.disabled=true;
frm.fwWebTuesday.disabled=true;
frm.fwWebWednesday.disabled=true;
frm.fwWebThursday.disabled=true;
frm.fwWebFriday.disabled=true;
frm.fwWebSaturday.disabled=true;

return true;
}

function enableDays()
{
frm=document.forms['EditForm'];

frm.fwWebSunday.disabled=false;
frm.fwWebMonday.disabled=false;
frm.fwWebTuesday.disabled=false;
frm.fwWebWednesday.disabled=false;
frm.fwWebThursday.disabled=false;
frm.fwWebFriday.disabled=false;
frm.fwWebSaturday.disabled=false;

return true;
}


function toggleDays(element)
{
if (element.checked)
    disableDays();
else
    enableDays();

return true;
}


function disableTime()
{
frm=document.forms['EditForm'];

disableDays();

frm.fwWebNotAnyday.disabled=true;
frm.fwWebHourStart.disabled=true;
frm.fwWebMinuteStart.disabled=true;
frm.fwWebSecondStart.disabled=true;
frm.fwWebHourEnd.disabled=true;
frm.fwWebMinuteEnd.disabled=true;
frm.fwWebSecondEnd.disabled=true;

return true;
}

function enableTime()
{
frm=document.forms['EditForm'];

enableDays();
frm.fwWebNotAnyday.disabled=false;
frm.fwWebHourStart.disabled=false;
frm.fwWebMinuteStart.disabled=false;
frm.fwWebSecondStart.disabled=false;
frm.fwWebHourEnd.disabled=false;
frm.fwWebMinuteEnd.disabled=false;
frm.fwWebSecondEnd.disabled=false;

return true;
}

function disableRateLimit()
{
frm=document.forms['EditForm'];

frm.fwWebRateNum.disabled=true;
frm.fwWebRateTime.disabled=true;

return true;
}

function enableRateLimit()
{
frm=document.forms['EditForm'];

frm.fwWebRateNum.disabled=false;
frm.fwWebRateTime.disabled=false;

return true;
}

function enablePort()
{
frm=document.forms['EditForm'];

frm.fwWebSrcPortStartDisplay.disabled=false;
frm.fwWebSrcPortEndDisplay.disabled=false;
frm.fwWebDstPortStartDisplay.disabled=false;
frm.fwWebDstPortEndDisplay.disabled=false;

return true;
}

function disablePort()
{
frm=document.forms['EditForm'];

frm.fwWebSrcPortStartDisplay.disabled=true;
frm.fwWebSrcPortEndDisplay.disabled=true;
frm.fwWebDstPortStartDisplay.disabled=true;
frm.fwWebDstPortEndDisplay.disabled=true;

return true;
}


//check the incoming and outgoing forms on submit
function checkInOutForm(form)
{
//alert("Incoming/Outgoing form being checked");


if (!form.fwWebEntrySelection) 
    {
    alert ("There is no rule entry!");
    return false;
    }
else
    {
    //alert ("There is a rule entry!");
    if (requireRadios (form, "fwWebEntrySelection", "the entries") == -1)
        return false;

    if (requireRadios (form, "fwWebAction", "what to apply") == -1)
        return false;
    }

return true;
}

//check the input form for editing or adding on submit
function checkEditForm(form)
{
// alert("Edit form being checked");

// check fwWebDirection
if (requireRadios (form, "fwWebDirection", "Direction") == -1)
    return false;

// check protocol selection
if (requireRadios (form, "fwWebProtoDisplay", "Protocol") == -1)
    return false;

// if you want to check rule name:
// - uncomment the next 2 lines of code, and
// - edit validateName() function to specify the chars NOT allowed in
//   rule name
// if (! validateName (form.fwWebNameDisplay))
//    return false;

// check all IP addresses
if( !checkAddr(form.fwWebSrcAddrStartDisplay) || !checkAddr(form.fwWebSrcAddrEndDisplay) || 
    !checkAddr(form.fwWebDstAddrStartDisplay) || !checkAddr(form.fwWebDstAddrEndDisplay) )
    return false;

// compare start and end IP addresses
if( CompareIP (form.fwWebSrcAddrStartDisplay.value, form.fwWebSrcAddrEndDisplay.value) > 0)
    {
    alert("Source start IP address greater than end IP address, please re-enter");
    return false;
    }

if( CompareIP (form.fwWebDstAddrStartDisplay.value, form.fwWebDstAddrEndDisplay.value) > 0)
    {
    alert("Destination start IP address greater than end IP address, please re-enter");
    return false;
    }

// check all port numbers 
if( !checkPort(form.fwWebSrcPortStartDisplay) || !checkPort(form.fwWebSrcPortEndDisplay) ||
    !checkPort(form.fwWebDstPortStartDisplay) || !checkPort(form.fwWebDstPortEndDisplay) )
    return false;

// compare start and end port numbers
if ( parseInt(form.fwWebSrcPortStartDisplay.value) > parseInt(form.fwWebSrcPortEndDisplay.value) )
    {
    alert("Source start port" + form.fwWebSrcPortStartDisplay.value+
          " greater than end port" + form.fwWebSrcPortEndDisplay.value +", please re-enter");
    return false;
    }

if ( parseInt(form.fwWebDstPortStartDisplay.value) > parseInt(form.fwWebDstPortEndDisplay.value) )
    {
    alert("Destination start port" + form.fwWebDstPortStartDisplay.value+
          " greater than end port" + form.fwWebDstPortEndDisplay.value +", please re-enter");
    return false;
    }

// check the anytime selection
var timeSelection = requireRadios (form, "fwWebNotAnytime", "Time of Day")

if (timeSelection == -1)
    return false;

// if select specific time of day, check the day and time input
if (timeSelection == 1)
    {
    if(!form.fwWebNotAnyday.checked && !form.fwWebSunday.checked && 
       !form.fwWebMonday.checked && !form.fwWebTuesday.checked && !form.fwWebWednesday.checked && 
       !form.fwWebThursday.checked && !form.fwWebFriday.checked && !form.fwWebSaturday.checked )
        {
        alert("You must select a day!");
        return false;
        }

    if ( !checkHour(form.fwWebHourStart) || !checkHour(form.fwWebHourEnd) || !checkMinute(form.fwWebMinuteStart) 
      || !checkMinute(form.fwWebMinuteEnd) || !checkSecond(form.fwWebSecondStart) || !checkSecond(form.fwWebSecondEnd) )
        return false;

    // compare time start and end value
    if ( parseInt(form.fwWebHourStart.value) > parseInt(form.fwWebHourEnd.value) )
        {
        alert("Hour start greater than hour end, please re-enter");
        return false;
        }
    else
    if ( parseInt(form.fwWebHourStart.value) == parseInt(form.fwWebHourEnd.value)
        && parseInt(form.fwWebMinuteStart.value) > parseInt(form.fwWebMinuteEnd.value) )
        {
        alert("Minute start greater than minute end, please re-enter");
        return false;
        }
    else 
        if ( parseInt(form.fwWebHourStart.value) == parseInt(form.fwWebHourEnd.value) 
            && parseInt(form.fwWebMinuteStart.value) == parseInt(form.fwWebMinuteEnd.value)
             && parseInt(form.fwWebSecondStart.value) > parseInt(form.fwWebSecondEnd.value) )
            {
            alert("Second start greater than second end, please re-enter");
            return false;
            }
    }

// check fwWebAction selection
if (requireRadios (form, "fwWebActionDisplay", "Action") == -1)
    return false;

// check log selection
if (requireRadios (form, "fwWebLogDisplay", "Log") == -1)
    return false; 
   
return true;
}

//check hour, munutes, seconds

function checkInteger(element)
{
//alert("checkInteger");
if( !isInteger(element.value) || parseInt(element.value) < 0 )
    {
    alert( "Please enter a integer greater than 0");
    element.focus();
    return false;
    }
return true;
}

//check hour, munutes, seconds

function checkHour(element)
{
//alert("hour");
if( !isInteger(element.value) || !inRange(element.value, 0, 23) )
    {
    alert( "Please enter a number beween 0 and  23 for hour");
    element.focus();
    return false;
    }
return true;
}


function checkMinute(element)
{
//alert("minute");
if( !isInteger(element.value) || !inRange(element.value, 0, 59) )
    {
    alert( "Please enter a number beween 0 and  59 for minute");
    element.focus();
    return false;
    }
return true;
}

function checkSecond(element)
{
//alert("second");
if( !isInteger(element.value) || !inRange(element.value, 0, 59) )
    {
    alert( "Please enter a number beween 0 and  59 for Second");
    element.focus();
    return false;
    }
return true;
}

function checkPort(element)
{
//alert( "checkport");
if( !isInteger(element.value) || !inRange(element.value, 0, 65535) )
    {
    alert( "Please enter a number beween 0 and 65535 for port number!");
    element.focus();
    return false;
    }
return true;
}

function CheckIPv6 (ip)
{
return true;
}

function CheckIPv4 (ip)
{
	var splitIP;
	splitIP = ip.split(".");

	// to check that there are dots between the numbers.
	if (splitIP.length == 1)		
		return false;
	// to check that there are only 3 dots between the numbers.
	else if (splitIP.length != 4)	
		return false;
	// to check that the each part of the ip has a length, i.e. no 2 dots after each other.
	else if ( (splitIP[0].length == 0) | (splitIP[1].length == 0) | (splitIP[2].length == 0) | (splitIP[3].length == 0) )
		return false;
	// to check that each part has a length not greater than 3 digits.
	else if ( (splitIP[0].length > 3) | (splitIP[1].length > 3) | (splitIP[2].length > 3) | (splitIP[3].length > 3) )
		return false;
	// to check on each part that it is integer only.
	else if ( !isInteger(splitIP[0]) | !isInteger(splitIP[1]) | !isInteger(splitIP[2]) | !isInteger(splitIP[3]) )
		return false;
	// to check on each part that it is not excceeding 255.
	else if ( (splitIP[0] > 255) | (splitIP[1] > 255) | (splitIP[2] > 255) | (splitIP[3] > 255) )
		return false;
	// to check on each part that it is not less than 0.
	else if ( (splitIP[0] < 0) | (splitIP[1] < 0) | (splitIP[2] < 0) | (splitIP[3] < 0) )
		return false;
	// otherwise return true, that is IP is valid.
	else
		return true;
}


function CompareIP (ip1, ip2)
{
form=document.forms['EditForm'];

//alert("CompareIP !");

if (form.fwWebDirection[2].checked || form.fwWebDirection[3].checked)
    {
//    alert ("CompareIP v6 addr");
    return(CompareIPV6(ip1, ip2));
    }
else
    {
//    alert("CompareIP v4 addr!");
    return (CompareIPV4 (ip1, ip2));
    }

// error case return
return 1;
}

function CompareIPV6 (ip1, ip2)
{
return -1;
}

function CompareIPV4 (ip1, ip2)
{
var splitIP1, splitIP2;
splitIP1 = ip1.split(".");
splitIP2 = ip2.split(".");


// to check on each part that it is not excceeding 255.
if ( parseInt(splitIP1[0]) > parseInt(splitIP2[0]) ) 
    return 1; //addr1's first part is grater
else if ( parseInt(splitIP1[0]) == parseInt(splitIP2[0]) ) 
    {
    if ( parseInt(splitIP1[1]) > parseInt(splitIP2[1]) ) 
        return 1; //addr1's second part is greater
    else if ( parseInt(splitIP1[1]) == parseInt(splitIP2[1]) )
        {
        if ( parseInt(splitIP1[2]) > parseInt(splitIP2[2]) ) 
            return 1; //addr1's third part is greater
        else if ( parseInt(splitIP1[2]) == parseInt(splitIP2[2]) )
            {
            if ( parseInt(splitIP1[3]) > parseInt(splitIP2[3]) ) 
                return 1; //addr1's fourth part is greater
            else if ( parseInt(splitIP1[3]) == parseInt(splitIP2[3]) ) 
                return 0; //addr1's all parts eaqual 
            else 
                return -1; //addr1's fourth part is less
            }
        else 
            return -1; //addr1's third part is less
        }
    else 
        return -1; //addr1's second part is less
    }
else 
    return -1; //addr1's first part is less

}



function checkReset()
{
enableTime();
enablePort();
return true;
}

function checkResetEdit()
{
form=document.forms['EditForm'];

enableTime();
enablePort();

if (form.fwWebNotAnytime[0].checked)
    disableTime();
else
    {
    if(form.fwWebNotAnyday.checked)
        disableDays();
    }

if (form.fwWebProtoDisplay[2].checked || form.fwWebProtoDisplay[3].checked)
    disablePort();

return true;
}