Solved

java script validation problem

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

12 Experts available now in Live!

Get 1:1 Help Now