Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 23rd, 2005, 06:33 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validate 3 textboxes

Hi All,

Please see codes below; if you copy and save it as .html, it will work. I was able to figure out(with some help) how to validate dates of 3 input text boxes. However, when an error message pops up, the cursor get tabbed to the next field. How do I set "focus" on the field with the error, and NOT get tabbed?

Thanks.

Leon


function Validatedate(InFeild)
{
IsValid = true;
date_format = "mm-dd-yy"
var emessage="";
//DATE FIELD CHECK
obj = eval("document.mainform." + InFeild);
fld_value = obj.value;

while (fld_value.indexOf("-") > -1)
  { fld_value = fld_value.replace("-","/");}

var date_split = fld_value.split("/")

while (fld_value.indexOf("/") > -1)
  { fld_value = fld_value.replace("/","");}

for (i = 0; i < fld_value.length; i++)
  {
  ch = fld_value.charCodeAt(i);
  if (ch < 48 || ch > 57)
     {IsValid = false;}
  }

for (i = 0; i < date_split.length; i++)
   {
   if (date_split[i].length == 1) {date_split[i] = "0" + date_split[i]}
   }
if (IsValid == false)
   {
   emessage=true;
   //semessage="Date format must be in mm/dd/yy";
   date_split = date_format.split("-");
   }
else if (date_split.length != 3)
   {
   emessage=true;
   //semessage="Date format must be in mm/dd/yy";
   date_split = date_format.split("-");
   }
else
   {
   if (parseInt(date_split[0]) > 31 || parseInt(date_split[1]) > 12)
       {
       IsValid = false;
       emessage=true;
       //semessage="Date format must be in mm/dd/yy";
       date_split = date_format.split("-");
       }
   }

fld_value = date_split[0] + "/" + date_split[1] + "/" + date_split[2];
//obj.value = fld_value;
if(emessage==true)
{
alert("Date format must be in mm/dd/yy");
return true;

}
}
// End -->
</script>


 
Old March 24th, 2005, 04:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I believe there is a focus() function that you call for the control.

Brian
 
Old March 25th, 2005, 02:08 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes. There is a focus() method, ie. document.mainform.fieldname.focus();

However, in my case, since I am validating 3 fields with one function, I don't know where the focus() should be insert, AND I don't know how to tell the fuction to focus on the field with the error. Make sense?

Thank you.

Leon


 
Old March 30th, 2005, 10:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

obj = eval("document.mainform." + InFeild);

This is where you get the object reference, right? If so, just do obj.focus(); in your javascript code.

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Two TextBoxes [email protected] ASP.NET 1.0 and 1.1 Basics 5 April 1st, 2006 06:32 AM
textboxes and additions toiletgraffiti C# 4 January 23rd, 2006 11:50 AM
Validate Multiple Textboxes ~Bean~ ASP.NET 1.0 and 1.1 Professional 2 October 11th, 2005 03:03 PM
Currency value in textboxes mahulda ASP.NET 1.0 and 1.1 Basics 1 April 6th, 2004 02:10 PM
Using Monthview with 2 textboxes eresina Beginning VB 6 0 October 2nd, 2003 08:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.