Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java script validation problem

Posted on 2011-03-08
3
Medium Priority
?
292 Views
Last Modified: 2012-08-14
I an another newbie stumbling around in java script and having a problem.

I need to validate two text boxes in an html form.  Each box needs to be checked for it's format, and checked to ensure that it actually contains data.

Box 1:  ID= nn-n; where n=a number from 0-9
Box 2: Date= yyyy-mm-dd; where y, m, and d =a number from 0-9

The script that I have so far is:

<script language="Javascript"><!--
function ValidateIDName(){
      var eTicket = document.eTW;
      var IDPat = /^([0-9])-([0-9])+$/;
      var errMsg = "";
      var setfocus = "";

      }
      if (eTicket['ID'].value == ""){
            errMsg = "Please enter your ID Number";
            setfocus = "['ID']";
      }
      var IDN = document.getElementById(%=ID.eTW%).value;
      var matchArray = IDN.match(IDPat);
      if (matchArray ==null)
      }
            errMsg = "Your ID Number is incorrect: Format should be: nn-n";
            setfocus = "['ID']";
      }
      if (eTicket['Name'].value == ""){
            errMsg = "Please enter your name";
            setfocus = "['Name']";
      }
      if (errMsg != ""){
            alert(errMsg);
            eval("eTicket" + setfocus + ".focus()");
      }
      else eTicket.submit();
 }//-->
 </script>

When "submit" is clicked, on a blank form, the form processes without any messages.

Could someone help me out?
0
Comment
Question by:dibrandt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35073282
Your curly brackets don't seem to match.  I'm not sure what %=ID.eTW% is in var IDN = document.getElementById(%=ID.eTW%).value;.

Fixed curly brackets.
<script language="Javascript"><!--
function ValidateIDName(){
      var eTicket = document.eTW;
      var IDPat = /^([0-9])-([0-9])+$/;
      var errMsg = "";
      var setfocus = "";

      if (eTicket['ID'].value == ""){
            errMsg = "Please enter your ID Number";
            setfocus = "['ID']";
      }
      var IDN = document.getElementById(%=ID.eTW%).value;
      var matchArray = IDN.match(IDPat);
      if (matchArray ==null){
            errMsg = "Your ID Number is incorrect: Format should be: nn-n";
            setfocus = "['ID']";
      }
      if (eTicket['Name'].value == ""){
            errMsg = "Please enter your name";
            setfocus = "['Name']";
      }
      if (errMsg != ""){
            alert(errMsg);
            eval("eTicket" + setfocus + ".focus()");
      }
      else eTicket.submit();
 }//-->
 </script>

Open in new window

0
 

Author Comment

by:dibrandt
ID: 35073791
I corrected the curly brackets, and the script still does not seem to be running.

The %=ID.eTW% is in var IDN = document.getElementById(%=ID.eTW%).value; - is my attempt to validate the format of "ID".  I need the format to be:

ID= nn-n; where n=a number from 0-9


0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35074173
OK. Please try the following changes:

<script language="Javascript"><!--
function ValidateIDName(){
      var eTicket = document.eTW;
      var IDPat = /^[0-9]{2}-[0-9]$/;
      var errMsg = "";
      var setfocus = "";

      if (eTicket['ID'].value == ""){
            errMsg = "Please enter your ID Number";
            setfocus = "['ID']";
      }
      if ( ! IDPat.test(eTicket['ID'].value) ){
            errMsg = "Your ID Number is incorrect: Format should be: nn-n";
            setfocus = "['ID']";
      }
      if (eTicket['Name'].value == ""){
            errMsg = "Please enter your name";
            setfocus = "['Name']";
      }
      if (errMsg != ""){
            alert(errMsg);
            eval("eTicket" + setfocus + ".focus()");
      }
      else eTicket.submit();
 }//-->
 </script>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re involved with your company’s wide area network (WAN), you’ve probably heard about SD-WANs. They’re the “boy wonder” of networking, ostensibly allowing companies to replace expensive MPLS lines with low-cost Internet access. But, are they …
Whoever said that “a picture is worth one thousand words” observed a fact that can dramatically affect your marketing success. Most people tend to learn visually, so many publishers commonly acknowledge the effectiveness of visual learning by using…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 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