Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java script validation problem

Posted on 2011-03-08
3
Medium Priority
?
294 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
  • 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

In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

879 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