Solved

java script validation problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Batch File to Telnet 10 3,292
Problem to the codes 19 99
Trying to format phone number on keyin. 5 85
CRM 2011 Opportunity Bug 18 141
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

19 Experts available now in Live!

Get 1:1 Help Now