Link to home
Start Free TrialLog in
Avatar of CCSOFlag
CCSOFlagFlag for United States of America

asked on

select dynamic textbox data

This is probably something simple I am missing.  I'm trying to create a validation checker for multiple elements.  I want to pass the element name into the function in javascript and if it doesn't pass the validation I want it to focus on the element and highlight the data in the element that is incorrect.  I want this done either onblur or onchange.

I haven't included all of the functions because the rest of it works.  In the javascript function the alert fires, but the focus and select do not.  it leaves the focus on the next element if you tab out or whatever element you click on next.
javascript:
function ValidateDate(varID)
{
alert('Please input a proper date with slashes');
var MyTextBox = document.getElementById(varID);
MyTextBox.focus();
MyTextBox.select();
///I've also tried this:
alert('Please input a proper date with slashes');
document.getElementById(varID).focus();
document.getElementById(varID).select();
}
 
HTML:
<form id="AddNewEmployeeform" >
    <input class="required" type="text" id="DateOfBirthTxt" name="DateOfBirthText" size="10" onchange="ValidateDate('DateOfBirthTxt');" />
</form>

Open in new window

Avatar of CCSOFlag
CCSOFlag
Flag of United States of America image

ASKER

Hmmm, so if I activate the function onload in the body tag it works.  Can you not do this with onblur or onchange?
my suggestion:

do not force the user to fill a field. You could have a two stage validation once on blur and then before the submission.

If done on blur since you are focusing back to the text box you could create an loop of alerts.
ASKER CERTIFIED SOLUTION
Avatar of kadaba
kadaba
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your suggestion works, but that's definitely not what I'm trying to do.  If I have to I guess I will just turn to a more complicated onsubmit validation, which is what I normally do.  I just didn't see why this wouldn't/shouldn't work.

Is there a particular reason why it won't allow focus on an onblur/onchange action?  Theoretically it shouldn't loop if it focuses back on that element.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, ok, that makes sense,but it doesn't loop for me.  It never focuses on the specified element.  It stays on the next element.  It validates, but doesn't focus.
oh, It does focus in IE now, but not in FF...  I use FF mainly.  Is this possibly a catch Firefox has to prevent loops?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I think I'll use some of what you gave and change a bit myself.  Appreeciate the info.