Solved

data validation for an MAC address by Java Script

Posted on 2007-03-27
43
634 Views
Last Modified: 2008-01-09
I have to develop a data validation for a MAC address. The MAC format should be "01:0A:95:77:C3:CE"

I have my following code


    <form name="IP_to_MAC" action="<portlet:actionURL/>" method="post" onSubmit="return ValidateForm()" >
     .....

                        Search for MAC Address: <input id="txtMAC" type="text" name="MACAddress" size="17" maxlength="25" onkeyup="toggleVisibility(2);" value ="00:0A:95:77:C3:CE">
 
   <input type='submit' value="Search" name="Search_MAC" onclick="return validate(document.getElementById('txtIP'));"  >
       

Do you have sample code for this validation?
0
Comment
Question by:wasabi3689
  • 23
  • 20
43 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Do you want to require the colon (:) to be entered or do you want something that will ignore it?

The function to validate all would be ...

function validMAC(val) {
   if (!val.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i) {
      alert('Not a valid MAC');
      return false;
   }
   return true;
}

Call the function inside your validating function with something like ...

if (!validMAC(document.IP_to_MAC.MACAddress.value)) {
    return false:
}

Let me know if you have a question about using it in your page or a question about the expression.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
colon (:) should be entered and validated
0
 

Author Comment

by:wasabi3689
Comment Utility
how can I combine your IP validation code with MAC validation code into the example?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
>> colon (:) should be entered and validated <<

The function I provided will do that.

You can take the script (without the function lines) and put it in ValidateForm().  If you provide your code I can show you where to insert it but it can go almost anywhere.  If you do use the script in the function and not as a separate function then you should make it ...

   val = document.IP_to_MAC.MACAddress.value;
   if (!val.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i) {
      alert('Not a valid MAC');
      return false;
   }

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
Here is the fucntion I use your code to put into


function ValidateForm(){
      var dt=document.IP_to_MAC.BegDate
        var dt2=document.IP_to_MAC.EndDate
       var dt3= document.IP_to_MAC.IPvalue
       
       val = document.IP_to_MAC.MACAddress.value;
         
      if (isDate(dt.value)==false)
        {
            dt.focus()
            return false
         }
         else if (isDate(dt2.value)==false)
         {
                dt2.focus()
            return false
      }
     
        else if (!val.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)
        {
            alert('Not a valid MAC address');
            return false;
        }
       
    return true
 }

Let me know if it's corrret. Is there a way to do the MAC validation before it sumbit, just like IP validation.

Also, when I click on submit button and one field got disabled, the program throw out an exception as

Caused by: java.lang.IllegalArgumentException: Render parameter key or value must not be null.

I can not submit to another page. Also, I don't see the MAC validation even I input a wrong MAC. How to fix it?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
I left off a closing parenthesis in the last example.  The code is giving a javascript error.  Sorry for the typo.  Try making the line ...

        else if (!val.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i))

If you want that validation to happen before submitting then you can use the onblur or onchange event in the input to call a function.  You will need to put that code in a function (see my first example [it also had the typo above]).

Unless you changed the code, you aren't validating the IP for submitting.  It is part of the submit process.  If it is still called by pressing the submit button then that makes it part of that process.  In fact, I mentioned this before, but that validation was being run twice (if I remember correctly).  That is one of the reasons I wanted to see all of the latest code for this page.  I was hoping to see that you had fixed that, especially if you are still having a problem getting the form to NOT submit when a part doesn't validate.

>> Also, when I click on submit button and one field got disabled, the program throw out an exception as <<
What do you mean disabled field?

Let me know how this works with the typo fixed.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
A spelling correction that will make it easier to read the last comment.

... you aren't validating the IP BEFORE submitting ...
0
 

Author Comment

by:wasabi3689
Comment Utility
your code is fine. But I come up a problem. I don't want to check both IP and MAC when I submit. It should be either or situation. What I want is MAC is checked when IP field is disabled. IP field is checked when MAC is disable. Here is my code


function ValidateForm(){
      var dt=document.IP_to_MAC.BegDate
        var dt2=document.IP_to_MAC.EndDate
       var dt3= document.IP_to_MAC.IPvalue
       
       val = document.IP_to_MAC.MACAddress.value;
         
      if (isDate(dt.value)==false)
        {
            dt.focus()
            return false
         }
         else if (isDate(dt2.value)==false)
         {
                dt2.focus()
            return false
      }
       
        else if  ( document.getElementById('txtMAC').disabled = false )
        {
             if (!val.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i))  
                {
                    alert('Not a valid MAC address');
                    return false;
                }        
         }      
       
    return true
 }
 
 
 function ValidateIntlNumber(fld) {
     if (fld.value.match(/[^0-9]/)) {
     
          alert ('Only numbers are permitted');
          // Un comment the next line is you want to remove the non-numbers automatically
          fld.value=fld.value.replace(/[^0-9]/g,'');
     }
}

 
 
</script>

             
 <script>
function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
                 
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtIP').disabled = false;
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
            }
      }
}
</script>

<script type="text/javascript">

function validate(f) {
    f.className="";
    var RegExPattern = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
   
    if ( document.getElementById('txtIP').disabled = false )
    {

            if ((f.value.match(RegExPattern)) && (f.value!='')) {
               // alert('good');
                document.getElementById("msg").innerHTML = "Valid IP";
                return true;
            }else if ( document.getElementById('txtMAC').disabled = true)
                {
                alert('Not a valid IP address');
                f.className = "fieldError";
                document.getElementById("txtIP").innerHTML = "Try Again";
                return false;
            }
     }
   
   
}

</script>

 <form name="IP_to_MAC" action="<portlet:actionURL/>" method="post" onSubmit="return ValidateForm()" >
       
               
               
            <div style="padding-top:5px;padding-bottom:5px;" id="ICC">
               
                        Search for MAC Address: <input id="txtMAC" type="text" name="MACAddress" size="17" maxlength="25" onkeyup="toggleVisibility(2);" >
 
                      - or -
                        Search for IP Address: <input id="txtIP" type="text" name="IPvalue" size="15" maxlength="25" onkeyup="toggleVisibility(1);" >

         
          </div>
         
    <input type='submit' value="Search" name="Search_MAC" onclick="return validate(document.getElementById('txtIP'));"  >
       

What I modify is here

else if  ( document.getElementById('txtMAC').disabled = false )
        {
             if (!val.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i))  
                {
                    alert('Not a valid MAC address');
                    return false;
                }        
         }    

and

else if ( document.getElementById('txtMAC').disabled = true)
                {
                alert('Not a valid IP address');
                f.className = "fieldError";
                document.getElementById("txtIP").innerHTML = "Try Again";
                return false;
            }  

But it doesn't work. How to fix?
0
 

Author Comment

by:wasabi3689
Comment Utility
hello, b0lsc0tt:, are you there?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Yes.  I saw your post but have not had time to look at it and it (the post and question) is sort of frustrating.  I was holding off replying but ... please don't take this wrong but what a mess the code is.  The whole thing seems like it is pieced together with no thought for the overall flow and purpose.  Maybe it is just incomplete (i.e. you still want to do stuff) but then fix that so you can see if that is the cause of the error.

If you can provide a specific error or narrow down what isn't working then I can try to fix it "as is" but personally I think you should really redo it.  Again, please don't take this personal because it REALLY isn't and I normally don't try to micro-manage how someone writes code.  I avoid it because there isn't usually one right way to do it.

Which do you want to do?  If you really want to keep the code as it is then look for a specific javascript error message or try using some alerts, etc to narrow down the problem.  If you don't mind redoing it then I have a couple of questions:

1.  Do you have to disable the fields or is the key to just check one?

2.  Do you want the check to be when submitted or when the value is entered?

3.  If disable, do you still want that to happen when a single key is pressed in one of the fields (i.e. disable the other)?

4.  Do you want the "error" to just be a javascript alert or do you want the message to show in the HTML? IP does both, MAC just one, they should be consistent.  This is part of what I meant about the code appearing to be only partially completed.

5.  What about other fields and things validated?  Depending on how much you want redone or help with it would be important to find out about other fields, etc because otherwise you will just have the same problem.

  I think those are the main things.  Like I said, I have only just quickly looked at it (and it has been a long day).  Even though this seems to be a new question and a lot to ask for 50 points I am not really bothered by that, especially since no other experts will be inconvenienced (i.e. "cheated" on points).  Please don't thing I am telling you to open a new question or that I can't/won't help.  That isn't the case at all.  The main point is to help you and I really hope this will do that.  (It may be long to read but it was an even bigger pain to write. :-] )

I will have to go offline for a bit but will be back and can help for at least a few hours still.  I look forward to your reply and let me know if you have a question about this.  

bol
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 50 total points
Comment Utility
I took a couple of minutes to make the improvements I was suggesting.  Take a look at the code below.  If the code you provided is just partial then I hope you can use the code below as an idea of how to clean it up.  The code will disable the other field and will only check the one that is enabled.  It will check when submitted (and only once). :)

<script type="text/javascript">

function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtIP').disabled = false;
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
            }
      }
}

function ValidateForm(){
      var dt1 = document.IP_to_MAC.BegDate;
      var dt2 = document.IP_to_MAC.EndDate;
      var dt3 = document.IP_to_MAC.IPvalue;
      var dt4 = document.IP_to_MAC.MACAddress;
      if (isDate(dt1.value)==false) {
            dt1.focus()
            return false
      } else if (isDate(dt2.value)==false) {
            dt2.focus()
            return false
      } else if (dt4.disabled = false) {
            if (!dt4.value.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)) {
                  alert('Not a valid MAC address');
                  dt4.focus();
                  return false;
            }        
      } else if (dt3.disabled = false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
      }      
      return true
}
 
function ValidateIntlNumber(fld) {
      if (fld.value.match(/[^0-9]/)) {
            alert ('Only numbers are permitted');
            // Un comment the next line is you want to remove the non-numbers automatically
            fld.value=fld.value.replace(/[^0-9]/g,'');
      }
}

function validate(f) {
      f.className="";
      var RegExPattern = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
   
      if ( document.getElementById('txtIP').disabled = false ) {
            if ((f.value.match(RegExPattern)) && (f.value!='')) {
                  // alert('good');
                  document.getElementById("msg").innerHTML = "Valid IP";
                  return true;
            } else if ( document.getElementById('txtMAC').disabled = true) {
                  alert('Not a valid IP address');
                  f.className = "fieldError";
                  document.getElementById("txtIP").innerHTML = "Try Again";
                  return false;
            }
      }
}
</script>

<form name="IP_to_MAC" action="<portlet:actionURL/>" method="post" onSubmit="return ValidateForm()" >
<div style="padding-top:5px;padding-bottom:5px;" id="ICC">
Search for MAC Address: <input id="txtMAC" type="text" name="MACAddress" size="17" maxlength="25" onkeyup="toggleVisibility(2);" >
- or -
Search for IP Address: <input id="txtIP" type="text" name="IPvalue" size="15" maxlength="25" onkeyup="toggleVisibility(1);" >
</div>
<input type="submit" value="Search" name="Search_MAC"  >

Let me know if you have a question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Notice I did change how some of the variables are named or used.  I also got rid of the extra event that was checking the IP separate from the main validation.  Now it is all one.

Let me know what you think.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
I use your code above, it throw out an exception when I click the button

Caused by: java.lang.IllegalArgumentException: Render parameter key or value must not be null.
      at org.apache.pluto.core.impl.ActionResponseImpl.setRenderParameter(ActionResponseImpl.java:162)
      at com.test.IP_to_MAC.processAction(IP_to_MAC.java:45)
      at org.apache.pluto.core.PortletServlet.dispatch(PortletServlet.java:216)
      at org.apache.pluto.core.PortletServlet.doPost(PortletServlet.java:150)

Can you double check if you miss any? I copy my code again

<script language = "Javascript">
....

function ValidateForm(){
      var dt1 = document.IP_to_MAC.BegDate;
      var dt2 = document.IP_to_MAC.EndDate;
      var dt3 = document.IP_to_MAC.IPvalue;
      var dt4 = document.IP_to_MAC.MACAddress;
      if (isDate(dt1.value)==false) {
            dt1.focus()
            return false
      } else if (isDate(dt2.value)==false) {
            dt2.focus()
            return false
      } else if (dt4.disabled = false) {
            if (!dt4.value.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)) {
                  alert('Not a valid MAC address');
                  dt4.focus();
                  return false;
            }        
      } else if (dt3.disabled = false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
      }      
      return true
}
 
 
 function ValidateIntlNumber(fld) {
     if (fld.value.match(/[^0-9]/)) {
     
          alert ('Only numbers are permitted');
          // Un comment the next line is you want to remove the non-numbers automatically
          fld.value=fld.value.replace(/[^0-9]/g,'');
     }
}

</script>

           
 <script>

function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtIP').disabled = false;
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
            }
      }
}

</script>


<script type="text/javascript">
   
function validate(f) {
      f.className="";
      var RegExPattern = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
   
      if ( document.getElementById('txtIP').disabled = false ) {
            if ((f.value.match(RegExPattern)) && (f.value!='')) {
                  // alert('good');
                  document.getElementById("msg").innerHTML = "Valid IP";
                  return true;
            } else if ( document.getElementById('txtMAC').disabled = true) {
                  alert('Not a valid IP address');
                  f.className = "fieldError";
                  document.getElementById("txtIP").innerHTML = "Try Again";
                  return false;
            }
      }
}

</script>

    <form name="IP_to_MAC" action="<portlet:actionURL/>" method="post" onSubmit="return ValidateForm()" >
                   
            <div style="padding-top:5px;padding-bottom:5px;" id="ICC">
               
                        Search for MAC Address: <input id="txtMAC" type="text" name="MACAddress" size="17" maxlength="25" onkeyup="toggleVisibility(2);" >
 
                      - or -
                        Search for IP Address: <input id="txtIP" type="text" name="IPvalue" size="15" maxlength="25" onkeyup="toggleVisibility(1);" >
          </div>
.....
   <input type="submit" value="Search" name="Search_MAC"  >
  .....
  </form>
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Sorry about that.  I did find an error.  I'll look through it once more just to make sure I didn't miss another.

There was a missing closing bracket in the code I corrected below.

      } else if (dt3.disabled = false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
            }      
      }      

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
either IP or MAC address validation doesn't work. I input either a wrong IP or MAC, the warning message doesn't pop up and automatically go to next page. It seems validation funcitons don't work
0
 

Author Comment

by:wasabi3689
Comment Utility
Also, I suspect the logic may be wrong for this code

} else if (dt4.disabled = false) {
            if (!dt4.value.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)) {
                  alert('Not a valid MAC address');
                  dt4.focus();
                  return false;
            }        
      } else if (dt3.disabled = false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
            }      
      }      

My logic is

when MAC field disabled, check IP validation. When IP filed disabled, check MAC validation
0
 

Author Comment

by:wasabi3689
Comment Utility
I just double check your logic. It seems fine. What is wrong with the code? why can not pick up the validation??
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
I copied a mistake from your script (I think) and missed it.  The problem is in the if in both cases there is just one equal sign, not 2.  We need 2.

} else if (dt4.disabled == false) {
            if (!dt4.value.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)) {
                  alert('Not a valid MAC address');
                  dt4.focus();
                  return false;
            }        
      } else if (dt3.disabled == false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
            }      
      }

I think the logic for the If's is fine.  dt4 is the field that has the MAC value.  If it isn't disabled (i.e. has a value) then you do the test for the MAC address.  dt3 also seems to be set up right.  The missing equal (i.e. using just 1) was a big problem that I missed.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
When input a wrong MAC, it pops up warning messege  - OK

When input a right MAC, it throws out an exception  - not OK

aused by: java.lang.IllegalArgumentException: Render parameter key or value must not be null.
      at org.apache.pluto.core.impl.ActionResponseImpl.setRenderParameter(ActionResponseImpl.java:162)
      at com.test.IP_to_MAC.processAction(IP_to_MAC.java:45)
      at org.apache.pluto.core.PortletServlet.dispatch(PortletServlet.java:216)
      at org.apache.pluto.core.PortletServlet.doPost(PortletServlet.java:150)

When input a wrong IP, no pop-up messege - not OK

When input a right IP, page stayed, not moved to next page

Still not work

0
 

Author Comment

by:wasabi3689
Comment Utility
OK here is the updated result

When input a wrong MAC, it pops up warning messege  - OK
When input a wrong IP, it pop-up messege - OK

Problem is

When input a right MAC, it throws out an exception  or
When input a right IP,  it throws out an exception

Caused by: java.lang.IllegalArgumentException: Render parameter key or value must not be null.
      at org.apache.pluto.core.impl.ActionResponseImpl.setRenderParameter(ActionResponseImpl.java:162)
      at com.test.IP_to_MAC.processAction(IP_to_MAC.java:44)
      at org.apache.pluto.core.PortletServlet.dispatch(PortletServlet.java:216)
      at org.apache.pluto.core.PortletServlet.doPost(PortletServlet.java:150)

It seems to me when one empty field got disabled, I can not submit to next page.
0
 

Author Comment

by:wasabi3689
Comment Utility
When a field got disabled, page can still be submitted?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
What browser are you using or where are you getting those error messages?  A little unusual.  I'm just curious.

It does seem to be related to disabling the fields.  Is that really something you want to do?  Let me know because, if not, then it will be simpler.  If you are concerned about a value in both you can test for that and alert to remove one.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
>> When a field got disabled, page can still be submitted? <<

Yes.  The disabled field is not sent, name or value (if exists) are not sent though.  In this case it doesn't matter.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
I use firefox molliza

When I input either right IP or MAC and click on submit button, it got the error message.

Of course, when either input IP or MAC, another field got disabled before it is submitted.
0
 

Author Comment

by:wasabi3689
Comment Utility
It will be ok to submit either IP or MAC empty field got disabled. No need both fields got value before it's submitted.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
OK.  I found the last of them (fingers crossed).  The validate() function had some issues and did some things it didn't need to.  I still have disabled in and it seems like the code (errors and success) will work.

I have copied the complete code again below but the major change was in the validate function.

<script type="text/javascript">

function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtIP').disabled = false;
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
            }
      }
}

function ValidateForm(){
      alert('0');
      var dt1 = document.IP_to_MAC.BegDate;
      var dt2 = document.IP_to_MAC.EndDate;
      var dt3 = document.IP_to_MAC.IPvalue;
      var dt4 = document.IP_to_MAC.MACAddress;
      if (isDate(dt1.value)==false) {
            dt1.focus()
            return false
      } else if (isDate(dt2.value)==false) {
            dt2.focus()
            return false
      } else if (dt4.disabled = false) {
            if (!dt4.value.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)) {
                  alert('Not a valid MAC address');
                  dt4.focus();
                  return false;
            }        
      } else if (dt3.disabled == false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
            }
      }
      return true;
}
 
function ValidateIntlNumber(fld) {
      if (fld.value.match(/[^0-9]/)) {
            alert ('Only numbers are permitted');
            // Un comment the next line is you want to remove the non-numbers automatically
            fld.value=fld.value.replace(/[^0-9]/g,'');
      }
}

function validate(f) {
      var RegExPattern = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
      if (!f.value.match(RegExPattern)) {
            alert('Not a valid IP address');
            return false;
      }
      return true;
}
</script>

<form name="IP_to_MAC" action="" method="post" onSubmit="return ValidateForm()" >
<div style="padding-top:5px;padding-bottom:5px;" id="ICC">
Search for MAC Address: <input id="txtMAC" type="text" name="MACAddress" size="17" maxlength="25" onkeyup="toggleVisibility(2);" >
- or -
Search for IP Address: <input id="txtIP" type="text" name="IPvalue" size="15" maxlength="25" onkeyup="toggleVisibility(1);" >
</div>
<input type="submit" value="Search" name="Search_MAC"  >

Let me know how that works.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
I left an alert in there.  At the top of validateForm() and you should delete it.  I missed that after deleting the rest.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
One other thing.  I made a page to test this but started with some old code.  The code I pasted has the line with only 1 equal sign for the MAC test.  Let me know if you want me to post all of the code again corrected but you should be able to find that easy.  Should be last problem.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
ok, please post your all code again. Thanks
0
 

Author Comment

by:wasabi3689
Comment Utility
I add one more = sign for
    } else if (dt4.disabled = false) {
I don't know if that is what you mean

I just find out more interesting testing

1. When I use cache right MAC ( stored in the field by the brower), not use keyboard to input MAC, it can go to next page.

2. When I use cache right IP ( stored in the field by the brower), not use keyboard to input IP, it pop up "Not a valid MAC", can not go to next page.

3. When I use both cache right IP and MAC input, it can go to next page.

4. use input right IP or MAC, it throw out an exception above


Hope above testing results can help you debug what is wrong with the code
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
>> ok, please post your all code again. Thanks <<
Oh, I wish you would have asked last night.  I will post it but I need to check real quick that I undo changes I need to make to run it.  The complete code is below.

<script type="text/javascript">

function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtIP').disabled = false;
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
            }
      }
}

function ValidateForm(){
      var dt1 = document.IP_to_MAC.BegDate;
      var dt2 = document.IP_to_MAC.EndDate;
      var dt3 = document.IP_to_MAC.IPvalue;
      var dt4 = document.IP_to_MAC.MACAddress;
      if (isDate(dt1.value)==false) {
            dt1.focus()
            return false
      } else if (isDate(dt2.value)==false) {
            dt2.focus()
            return false
      } else if (dt4.disabled == false) {
            if (!dt4.value.match(/^([A-F0-9]{2}:){5}[A-F0-9]{2}$/i)) {
                  alert('Not a valid MAC address');
                  dt4.focus();
                  return false;
            }        
      } else if (dt3.disabled == false) {
            if (!validate(dt3)) {
                  dt3.focus();
                  return false;
            }
      }
      return true;
}
 
function ValidateIntlNumber(fld) {
      if (fld.value.match(/[^0-9]/)) {
            alert ('Only numbers are permitted');
            // Un comment the next line is you want to remove the non-numbers automatically
            fld.value=fld.value.replace(/[^0-9]/g,'');
      }
}

function validate(f) {
      var RegExPattern = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
      if (!f.value.match(RegExPattern)) {
            alert('Not a valid IP address');
            return false;
      }
      return true;
}
</script>

<form name="IP_to_MAC" action="" method="post" onSubmit="return ValidateForm()" >
<div style="padding-top:5px;padding-bottom:5px;" id="ICC">
Search for MAC Address: <input id="txtMAC" type="text" name="MACAddress" size="17" maxlength="25" onkeyup="toggleVisibility(2);" >
- or -
Search for IP Address: <input id="txtIP" type="text" name="IPvalue" size="15" maxlength="25" onkeyup="toggleVisibility(1);" >
</div>
<input type="submit" value="Search" name="Search_MAC"  >

Let me know how that works.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
Here is the testing result


1. When I use cache right MAC ( stored in the field by the brower), not use keyboard to input MAC, it can go to next page.

2. When I use cache right IP ( stored in the field by the brower), not use keyboard to input IP, it pop up "Not a valid MAC", can not go to next page.

3. When I use both cache right IP and MAC input, it can go to next page.

4. use input right IP or MAC, it throw out an exception

Caused by: java.lang.IllegalArgumentException: Render parameter key or value must not be null.
      at org.apache.pluto.core.impl.ActionResponseImpl.setRenderParameter(ActionResponseImpl.java:162)
      at com.test.IP_to_MAC.processAction(IP_to_MAC.java:45)
      at org.apache.pluto.core.PortletServlet.dispatch(PortletServlet.java:216)
      at org.apache.pluto.core.PortletServlet.doPost(PortletServlet.java:150)

The major problem is I cannot go to next page when I input a right IP or MAC.

0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
What do you have for the value in the action attribute in the form tag (see below)?  For my testing I had to remove what you had.  Have you reentered what you need?  I completely forgot about changing it.

<form name="IP_to_MAC" action="" method="post" onSubmit="return ValidateForm()" >

You need to have something there (action="") for the page to submit.  The first 3 situations you mention are odd or I don't understand them.  Make sure you have reloaded the page.  If the browser is using a cached version of the page then you may be looking at old problems.  Look at the HTML source of the page, especially the validate() function, and make sure it matches the latest code.  If it doesn't the browser is using a cached version and you need to force it to refresh.  The second error just shouldn't be happening with the current code.

If the above doesn't help to fix it then try adding some alerts to narrow down what is happening and where the script is going.

If that doesn't help then post your current code.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
I put "action="<portlet:actionURL/>", it's kind a different from action="" in classic JSP

This project is portlet project. When click "submit", that should be the way it goes to next page.

But I can try it and remove the action, see if it's different.
0
 

Author Comment

by:wasabi3689
Comment Utility
I remove "action="<portlet:actionURL/>" to test, it stays in same page when I click submit
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
>> I remove "action="<portlet:actionURL/>" to test, it stays in same page when I click submit  <<
Like I said, you need an action.  It looks like that wasn't a problem.  If it wasn't then you probably didn't copy my code in as a block and that makes me wonder if the error is because you didn't update part.  I don't know JSP at all so I can't say if that is the problem or not.  Just out of curiousity do you actually see 'action="<portlet:actionURL/>"' in the html source too or is that just the code?

Did you view the browser source to make sure it has the latest changes, etc?  Did you try to add alerts?  I did provide other suggestions in case it wasn't just the action and you can try those.  If the problem seems to be JSP related then I will not be any help and you would need to start a new question in a better topic area to get the help you need.

Let me know if you have a question.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
Let me do some debugging for my code. Then I will tell you if the problem come from "<portlet:actionURL/>"
0
 

Author Comment

by:wasabi3689
Comment Utility
I make it -:). The problem is in  my java code. Once I comment out MAC and IP paratmeters rending. It works now.

I accept your solution. Thank you
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Great!  I'm glad it is working and I could help.  Thanks for the grade, the points and the fun question.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
sorry to bother you again. I want to set a background color to the field got disabled. I put the code as follow


function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtMAC').style.bgcolor = "red";
                  document.getElementById('txtIP').disabled = false;
                 
                 
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
                  document.getElementById('txtIP').style.bgcolor = "red";
            }
      }
}

But it doesn't work. Can you tell what's wrong?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
This is a diffenent issue you know.  I am starting to feel abused.  I'll never get my next certification this way.  :)

You need to use backgroundColor.  For example ...

function toggleVisibility(str) {
      if (str == 1) {
            if (document.getElementById('txtIP').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
                  document.getElementById('txtIP').style.backgroundColor = "white";
                  document.getElementById('txtMAC').style.backgroundColor = "white";
            } else {
                  document.getElementById('txtMAC').disabled = true;
                  document.getElementById('txtIP').disabled = false;
                  document.getElementById('txtMAC').style.backgroundColor = "red";
            }
      } else if (str == 2) {
            if (document.getElementById('txtMAC').value == "") {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = false;
                  document.getElementById('txtIP').style.backgroundColor = "white";
                  document.getElementById('txtMAC').style.backgroundColor = "white";
            } else {
                  document.getElementById('txtMAC').disabled = false;
                  document.getElementById('txtIP').disabled = true;
                  document.getElementById('txtIP').style.backgroundColor = "red";
            }
      }
}

Notice I also added lines to change the color back (to white).  You can delete those if you don't want them.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
It does look cool though and I like the cleaner code.

bol
0
 

Author Comment

by:wasabi3689
Comment Utility
excellent! it work! thank you again
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now