473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Javascript onfocusout problem

20
Hi,

I have placed two text boxes(server side) for Name and Age in asp.net 2.0.
Their ids are txtPatientName and txtPatientAge.
The validation to be done is both text boxes should not be left empty.

I have written two javascript functions as follows.
Expand|Select|Wrap|Line Numbers
  1. function isName() 
  2.     {
  3.         var str = document.forms[0].txtPatientName.value;
  4.         if (str == "") 
  5.             {
  6.                 document.forms[0].txtPatientName.focus();
  7.                 alert("The NAME field is blank.Please enter name.")
  8.  
  9.                 return true;
  10.             }
  11.   }
  12.  
  13. function age()
  14.     {
  15.       var age=document.forms[0].txtPatientAge.value;
  16.        var name=document.forms[0].txtPatientName.value;
  17.       if (age== "") 
  18.             {    
  19.                 alert("The age field should not be blank");
  20.                 return false;      
  21.             }
  22. }
  23.  
In default.aspx.cs file, I have written
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.  
  4.  
  5.         if (Page.IsPostBack == false)
  6.         {
  7.             txtPatientName.Attributes.Add("onfocusout", "javascript:isName()");
  8.             txtPatientAge.Attributes.Add("onfocusout", "javascript:age()");
  9. }
  10.  
Problem I am getting: When name is not given and if we click TAB,it is first showing alert as “age should not be blank” and then it is showing alert as “name should not be blank”.

What I want is:When name is not given and if we click TAB, then it should show alert as “name should not be blank” and focus should be on “name” textbox.Then it goes to age textbox.
After this, When age is not given and if we click TAB, then should show alert as “age should not be blank” and focus should be on “age” textbox.


Plz help me in this regard by sending sample code.

Thanks
Silpa
Jun 8 '07 #1
2 5023
Hi,
Try to do like this i think this will work as you want


function ------- ()
{
if (document.getElementById("txtname").value=="")
{
alert("Please Enter Name");
return;
}
else
{
if(document.getElementById("txtage").value=="")
{
alert("Please Enter Age");
return;
}
}
Jun 8 '07 #2
Frinavale
9,735 Expert Mod 8TB
Hi there,
The problem you're having is that you're setting the focus on the text box before you are calling the alert(). When you call the alert() your focus is changed to the alert box...it's undoing your focus().

-Frinny
Jun 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: kaeli | last post by:
All, I've been unable to find out if javascript supports for (var e in obj) type of looping syntax. Does it? If so, is this for DOM browsers only? TIA! --
13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
6
by: Andy Fish | last post by:
Hi, I want to use an anchor tag to invoke some javascript and I've read that it's bad form to use <a href="javascript:foo()"> I've read endless usenet posts and hint sites on the net, they all...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
3
by: bbass | last post by:
below is the snippet of the code: <a href="merc.htm" target="_new_merc" onfocusout=window.close class="left_link"> I have an event inside the <a> tag onfocusout=window.close the w3...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.