Solved

java script validation problem

Posted on 2011-03-08
3
285 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Riverbed Technology's webinar discusses networking for the cloud era with simplified SD-WAN cloud connectivity.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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