Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onchange Nastiness 3

Status
Not open for further replies.

Signit

MIS
Oct 17, 2003
114
US
I am verifying data in a text field with an onchange event. After the user puts in a machine name I check to verify that said machine actually exists (DB call). This works great except I don't like the user experience in a specific instance and I am wondering if anyone has any ideas on how to get around it. If the user puts in a machine name and then goes straight for the "submit" button w/o filling out any other fields (other fields are optional) the onchange event for the text field fires and evaluates the machine name. If it evaluates correctly they have to reclick the "submit" button before the application will move on. It is not as bad if they put in a bad machine name as they get alert. I am trying to avoid using a check this machine's name button, but will if I have to. Does anyone see another way around this issue?
 
why not check the machine name on the server side too, so if it is submitted with just a valid machine name, you can redirect to the next step on the server side.


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
can you post a sample of your code?

After the user puts in a machine name I check to verify that said machine actually exists (DB call)
how's this called? i'm sure the js itself doesn't connect to the db, there must be an intermediary server call. in which case, are you using XML_HTTP_REQUEST, or something involving a redirect, or framesets?

the form's behaviour depends on what your code is trying to do. for example, if onchange invokes alert('foo'), the form submission is cancelled.

<marc>
New to Tek-Tips? Get better answers - faq581-3339
 
The onchange fires off a popup window that does the check (this is for our intranet so popups are okay).
 
I imagine the easiest fix would be to take the popup code, and rework it to use an XMT_HTTP_REQUEST object (the X in AJAX) instead of a popup.

The alternative would be to change the onchange event so it detects if the current focus is on the submit button (before firing the popup - if it is, break).

Once you've fired the popup, you're fairly stuck...you could detect at the end of the popup code if the submit button has the focus (in which case programatically fire the form submit action), but the first (AJAX) solution is the most elegant (for future maintainability), and probably the simplest.

<marc>
New to Tek-Tips? Get better answers - faq581-3339
 
The DB check might go faster if you put it in an invisible IFRAME. When the check is complete, fire an alert if the check failed or un-disable (um... I guess, "enable") the Submit button if the check passed.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
LookingForInfo said:
un-disable (um... I guess, "enable") the Submit button if the check passed.
the onchange event doesn't fire until after the user has clicked out of the text box. this means that the user won't be able to click the submit button without clicking somewhere else on the page first (or thinking that the computer name is invalid, because the submit button won't enable - they may not realise you need to click elsewhere first).

<marc>
New to Tek-Tips? Get better answers - faq581-3339
 
You just have a case of event collision.
The onchange event fires and interupts the submit button event. Is your submit button using type="submit" or an onclick event?
The onchange event only fires when focus goes elsewhere, in this case to the submit button so you have two events that want to fire at the same time and onchange wins canceling out the submit button click.

You could use an onkeyup event instead. Every key they press will fire your validation of that field but only as the key is released. Any move to click the submit button would be after the onkeyup event has already occured instead of colliding with the onchange event like it is doing now.
Using onkeyup means you will have to consider what happens if the validation fails. You would not want an alert each time a key is pressed. :)

Another method is to not trigger the validation until after submit is pressed. Like this:

<form name="myform" method="post" action="mypage.asp" onsubmit="return validatefields()">
stuff in here....
Machine Name:<input type="text" id="machinename">
stuff in here...
<input type="submit" value="Submit">
</form>
<script language="text/javascript">
function validatefields()
{
var mname = document.getElementById('machinename').value;
if (mname MEETS MY CRITERIA)
return true;
else
return false;
}

Using this method validation occurs after submit is pressed and if the value does not meet your criteria the submission is halted.



Paranoid? ME?? WHO WANTS TO KNOW????
 
manarth said:
they may not realise you need to click elsewhere first

Good point. Okay, so change the Submit button to a regular button. Onclick starts the form validation and runs the JSP in the invisible IFRAME to check the entered value. Meanwhile, the JavaScript goes into a holding pattern (using JavaScript's setInterval(...) function) until the JSP is done. Once the JavaScript continues, it does one of two things, based on the results of the JSP check.

Note that this is very similar to what theniteowl said. The JSP would be called where theniteowl wrote
Code:
mname MEETS MY CRITERIA
.

This is very general. Does this make sense or sound like something you'd like to try?

Something else you can do is do the database hit on the way in. If there is a reasonably small number of valid values the user can enter, collect them as the page is being drawn and store them in an asterisk-delimeted list. Then, when validating the form prior to submission, check if the string of valid names includes "*"+userResponse+"*". If it does, then it's valid.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Unfortunately the value the user is selecting can be 1 in 60,000 (give or a take a few). I went with the idea of checking the value in an iframe. Worked out pretty well. Thank you to all for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top