473,396 Members | 1,724 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,396 software developers and data experts.

how to set focus to the same txtbox after validation by onblur which calls a function

my textbox and the function in javascript
Expand|Select|Wrap|Line Numbers
  1. <td><input type="text" name="username" id="username" onblur="validate('text','username','user')">
  2. <div id="user"></div></td>
  3.  
  4. <script type="javascript">
  5. function validate(type,fieldname,divid)
  6. {
  7. var txt_value=document.getElementById(fieldname).value;
  8. if(type=='text')
  9. {
  10.    if(txt_value==""||txt_value==null)
  11.    {
  12.     document.getElementById(divid).innerHTML="<span   style='color:red;'>Mandatory!</span>"
  13.     document.getElementById(fieldname).focus();
  14.     return false;
  15.    }
  16. }
  17. }
  18. </script>
  19.  
  20.  
Soon after the textbox loses the focus the error message should be displayed and the focus has to be set back to the textbox. Likewise i'm doing for different txtbox on various condition.

Here the error message is displayed but the focus is not set to the textbox again moving to the textbox which I clicked. It should not allow me to move to the next field rather it should display the error message and focus on the same textbox.

Help me.
Thanks,
vigneshwarie.
Sep 26 '10 #1
1 11252
Dormilich
8,658 Expert Mod 8TB
the focus() doesn’t work, because you’ve not yet left the <input> element.

comments:
- line 4: wrong MIME
- line 7/8: you don’t need to pass the input id and input type, as this is available through this
- line 10: the input’s value is always a string, no need to test for null

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="username" id="username" onblur="validate(this,'user')">
  2. <div id="user"></div>
  3.  
  4. <script type="text/javascript">
  5. function validate(elem,divid)
  6. {
  7.    if(elem.type !== 'text') {
  8.       return true;
  9.    }
  10.    if(elem.value.length === 0) {
  11.       document.getElementById(divid).innerHTML = "<span style='color:red;'>Mandatory!</span>";
  12.       return false;
  13.    }
  14.    return true;
  15. }
  16. </script>
  17.  
  18.  
  19.  
Sep 26 '10 #2

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

Similar topics

2
by: fish | last post by:
Hi, I have an HTML page with a FORM and some input fields. On the fields I wish to do validation as the punters change the field values. If they get it wrong, then I tell them and then wish...
3
by: Dan | last post by:
This function validates a price field on my form. It fires with onchange. If the validation fails, I want the focus to return to the same field so that the user can correct it. The way it is...
9
by: Roger Withnell | last post by:
Tearing hair out time! Simple attached page shows the problem. http://www.brilley.co.uk/TestFocusSelect.htm Using a function to test if too many characters have been keyed in to a textarea....
2
by: Vinny | last post by:
I'm writing a small ASP program which prompts the user for data. I would like to interactively validate the data as they are entering it, rather than waiting until they submit the form. So, I am...
2
by: Vinny | last post by:
A customer wants me to convert a VB app I wrote for them into a web app for remote users to do data entry. So, I'm writing a small ASP program which prompts the user for data, much like a VB...
1
by: paulsson | last post by:
Does anyone know why placing the cursor/focus in a textfield in IE would cause the window.onblur event handler to be called? This seems very odd since the window still has focus.... Is there a way...
3
by: Theadmin77 | last post by:
I dont know what i am doing wrong , i am trying to validate a string entry (first name to only take lower and upper case letters) thru a function , but for some reason is not working ....=0( Any...
4
by: planb | last post by:
Hi, I'd like to have a rollover like effect when a input field has the focus, any idea of how to do this with just CSS (easy enough with javascript)? What I'm thinking of is having a tips box...
3
by: Taylor Smith | last post by:
I have a validation file and i do not know where to add the focus so that with each error it focuses on the field where the error occured. Maybe someone will have a better idea on how to do this....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.