Solved

Javascript validation not working on every field

Posted on 2012-12-28
5
320 Views
Last Modified: 2013-01-08
The code below is a section of a webpage that validates IP Addresses.  The code is only checking the first text box on each record.  Any ideas why it is not checking all text boxes?

Thanks,
David


<html>
<head>

<script type="text/javascript">
<!--
 
         var octet = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])';
        var ip    = '(?:' + octet + '\\.){3}' + octet;
        var ipRE  = new RegExp( '^' + ip + '$' );

        function check_ip_format( ip_field, ip_value ) {
                alert("ip_field=>>" + ip_field.name + "<<");
              alert("ip_value=>>" + ip_value + "<<");
              if (!isBlank(ip_value)){
                    alert( ( ipRE.test( ip_value ) ? '' : 'in' ) + 'valid' );
              
                    if ( !ipRE.test( ip_value ) )
                          ip_field.focus();
              }
        }

    function isBlank(val)
    {
        if (val == null) { return true; }
        for (var i = 0; i < val.length; i++) {
            if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) { return false; }
        }
        return true;
    }
      
//-->
</script>
<title>Users</title>
      <link href="includes/css/main.css" rel="stylesheet">
</head>
<body onload="IPMin_1.focus();">
<table cellpadding="0" cellspacing="0" border="0" align="left" width="100%">      

      <form action="" name="form" method="post">

      <tr><td>&nbsp;</td></tr>
      <tr>
            <td>
                                    <table border="0" cellspacing="3" cellpadding="0" width="855">
                                          <tr>
                                                <td colspan="5"><hr /></td>
                                          </tr>
                                          <tr><td colspan="2">&nbsp;</td></tr>

                                          <tr>
                                                <td colspan="2">
                                                      <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                                            <tr>
                                                                <td class="listheader" width="5%"><b>Record</b></td>      
                                                                <td class="listheader" width="2%">&nbsp;</td>
                                                                <td class="listheader" width="20%"><b>IP Address Minimum</b></td>
                                                                <td class="listheader" width="2%">&nbsp;</td>                                          
                                                                <td class="listheader" width="20%"><b>IP Address Maximum</b></td>      
                                                                <td class="listheader" width="2%">&nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td colspan="6"><hr /></td>
                                                            </tr>

                                                          <tr>
                                                                <td class="formlabel" width="5%">1</td>
                                                          <td class="listheader" width="2%">&nbsp;</td>
                                                                <td nowrap class="formdata" width="20%">
                                                                    <input class="formdata" name="IPMin_1" type="text" size="20" maxlength="25" value="98.191.209.200" onblur="javascript: return check_ip_format(IPMin_1, IPMin_1.value);" />
                                                                </td>
                                                          <td class="listheader" width="2%">&nbsp;</td>
                                                                <td nowrap class="formdata" width="20%">
                                                                      <input class="formdata" name="IPMax_1" type="text" size="20" maxlength="25" value="98.191.209.250" onblur="javascript: return check_ip_format(IPMax_1, IPMax_1.value);" />
                                                                </td>
                                                          </tr>
                               
                                                          <tr>
                                                                <td class="formlabel" width="5%">2</td>
                                                          <td class="listheader" width="2%">&nbsp;</td>
                                                                <td nowrap class="formdata" width="20%">
                                                                    <input class="formdata" name="IPMin_2" type="text" size="20" maxlength="25" value="98.191.209.199" onblur="javascript: return check_ip_format(IPMin_2, IPMin_2.value);" />
                                                                </td>
                                                          <td class="listheader" width="2%">&nbsp;</td>
                                                                <td nowrap class="formdata" width="20%">
                                                                      <input class="formdata" name="IPMax_2" type="text" size="20" maxlength="25" value="98.191.209.255" onblur="javascript: return check_ip_format(IPMax_2, IPMax_2.value);" />
                                                                </td>
                                                          </tr>
                               
                                                            <tr><td colspan="5">&nbsp;</td></tr>
                                                            <tr><td colspan="5">&nbsp;</td></tr>
                                                      </table>
                                                </td>
                                          </tr>
                                    </table>
            </td>
      </tr>                        

      </form>
</table>

</body>
</html>
0
Comment
Question by:skinsfan99
  • 3
5 Comments
 
LVL 10

Expert Comment

by:ienaxxx
Comment Utility
try sobstituting the "alert" command with something else (you can even comment it).
Alert command behave strange in the script flow.

and try to remove the "return" part in ur onblur attributes: it's not needed IMO
0
 
LVL 10

Accepted Solution

by:
ienaxxx earned 500 total points
Comment Utility
It's working out-of-the-box in IE9 and in firefox 17.0.1 !!
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
It checks only one text box at a time because it is programmed to work only on the text box that looses the focus.  Try the function at the end of this post.  If you call it as:
<input onblur="check_ip_format(this);" ..>

it will validate only THAT text box (which is equivalent to what you currently have).

If you call it as:
<input onblur="check_ip_format(this,'TD');" ..>
It will validate all the <INPUT> elements within the enclosing <TD>.  So if you had:
<TD>
 <INPUT name="IP...">
 <INPUT name="IP...">
</TD>
it will validate BOTH <INPUT>s (as long as the names begin with "IP").

Similarly, if you call it as:
<input onblur="check_ip_format(this,'TR');" ..>
it will validate all the inputs on that row (which I guess is what you are after).

Finally,
calling it as:
<input onblur="check_ip_format(this,'TABLE');" ..>
will validate all the <INPUT>s in the entire page (again, as long as the name of each field starts with "IP").

        function check_ip_format( field, container ) {
			var nodes=null, limit, result='',i=-1;

			container=container?container.toUpperCase():null;
			if(container)
			{
				while( field.nodeName.toUpperCase()!=container )
				{
					field=field.parentNode;
				}

				nodes=field.getElementsByTagName('input');
			}
			else
			{
				nodes=[field];
			}

			limit=nodes.length;
			while(++i<limit)
			{
				//validate only TEXT fields whose name start with "IP"
				if(nodes[i].type && nodes[i].type.toUpperCase()=="TEXT" && nodes[i].name.substring(0,2)=='IP')
				{
					result+=("ip_field=>>" + nodes[i].name + "<<\n");
					result+=("ip_value=>>" + nodes[i].value + "<<\n");
					if (!isBlank(nodes[i].value)){
						result+=( ( ipRE.test( nodes[i].value ) ? '' : 'in' ) + 'valid' )+"\n\n";
              
						if ( !ipRE.test( nodes[i].value ) )
							nodes[i].focus();
					}
				}
			}
			alert( result );
		}

Open in new window


Regards,
Hielo
0
 

Author Closing Comment

by:skinsfan99
Comment Utility
You were correct, the issue was the browser I was using and not the code.  I use Google Chrome most of the time.  It appears that JavaScript doesn't work or is limited in Google Chrome.  Once I tested under IE and Firefox everything worked as expected.  Some minor tweeks and validation is working 100%.  I need to get myself better educated on Google Chrome and it's limitations.  Thank you!!
0
 
LVL 10

Expert Comment

by:ienaxxx
Comment Utility
Glad to be of help. :-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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…

763 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