Link to home
Start Free TrialLog in
Avatar of wasabi3689
wasabi3689Flag for United States of America

asked on

data validation for an MAC address by Java Script

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?
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

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
Avatar of wasabi3689

ASKER

colon (:) should be entered and validated
how can I combine your IP validation code with MAC validation code into the example?
>> 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
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?
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
A spelling correction that will make it easier to read the last comment.

... you aren't validating the IP BEFORE submitting ...
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?
hello, b0lsc0tt:, are you there?
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
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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>
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
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
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
I just double check your logic. It seems fine. What is wrong with the code? why can not pick up the validation??
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
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

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.
When a field got disabled, page can still be submitted?
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
>> 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
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.
It will be ok to submit either IP or MAC empty field got disabled. No need both fields got value before it's submitted.
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
I left an alert in there.  At the top of validateForm() and you should delete it.  I missed that after deleting the rest.

bol
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
ok, please post your all code again. Thanks
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
>> 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
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.

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
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.
I remove "action="<portlet:actionURL/>" to test, it stays in same page when I click submit
>> 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
Let me do some debugging for my code. Then I will tell you if the problem come from "<portlet:actionURL/>"
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
Great!  I'm glad it is working and I could help.  Thanks for the grade, the points and the fun question.

bol
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?
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
It does look cool though and I like the cleaner code.

bol
excellent! it work! thank you again