|
Using jquery to check value of text field, compare to string, fire event
09-09-2009, 06:40 PM
|
Using jquery to check value of text field, compare to string, fire event
|
Posts: 65
Location: san francisco, ca
|
I'd like to use jquery validation plugin to take a value a user keys into a text field, compare that value to a specific clear text string elsewhere on the page, then display a message.
From my research, the event is either blur(fn) or onfocusout on that text field.
Its just the function that i am having trouble finding assistance on.
My page has a "Primary Email" displayed in a text field (popped by DB).
Below that, a flat text display of "Email used for Alerts", also from DB.
These 2 email strings can be either same or different.
If/when user changes primary email string (in text field), then leaves that text field, i want to check that value & compare it to the flat text email string.
Basically, if "primary email" <> "email used for alerts", then display message on front end asking if they'd like to update their alert email address to same as their newly-keyed primary.
Getting an event to fire on exit of the field seems straight forward.
Getting the DIV with the question to show/hide is also straight forward.
But its the function called by the exit from the field that has me flummoxed.
I'm continuing to look on the internets. But this forum has been so good to me in the past, i feel i owe y'all a case a scotch.
thanks...
|
|
|
|
09-09-2009, 07:03 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
OnBlur() is the event that triggers when an object loses focus either by a tab keypress or a mouse action to focus another object.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
09-09-2009, 07:06 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
I don't know what onfocusout is. What you're looking for is the blur(fn) event. This fires whenever an element loses focus.
Did you look into the keyup(fn) event? This fires every time a key is released, and contains an event.keyCode which shows which key has been released. Also, you can just read the string from the textarea to see what has been produced so far. http://docs.jquery.com/Events/keyup
|
|
|
|
09-09-2009, 07:28 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 65
Location: san francisco, ca
|
what i hoped to say in my OP was that i am finding plenty of options firing the event, with "blur(fn)" and "onfocusout" being those most commonly referenced by those using jquery on form elements.
its the function i'd call that i cannot locate. the function is not an auto-complete, where the users value for "Primary Email" is compared upon each character.
I need the whole value they entered for "Primary Email" to be compared after that field loses focus.
Think: password / confirm password. value entered for confirm pw must be compared to value in (main) pw. if pw = cpw, do something. if pw <> cpw, do something else.
craaap. thats a great place to look next for this blasted function.
thanks for the replies!
|
|
|
|
09-09-2009, 07:48 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
ok, there's only 3 steps.
1. get the value of both fields
2. compare for equality
3. do something
Which part are you having trouble with?
|
|
|
|
09-09-2009, 07:50 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
Quote:
|
Think: password / confirm password. value entered for confirm pw must be compared to value in (main) pw. if pw = cpw, do something. if pw <> cpw, do something else.
|
such checks SHOULD be done server side NOT with client side javascript
you use the blur event to trigger your function/method.
If you want to compare character by character you would use the onChange() event or capture the keyboard events (onKeyDown, onKeyUp, onKeyPress)
there is no DOM method for character by character comparison of a string/input field. You would have to write this yourself and call it on a standard DOM event or prototype it to a new event/method.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
09-10-2009, 03:32 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 65
Location: san francisco, ca
|
@wayfarer07:
I was looking for the specific syntax of the JS function that did all three of those steps. Something i could find working on the web or provided here, that i could play with, mod, reverse, learn.
@chrishirst:
I am creating a prototype for client to show functionality, behaviour, layout, etc. My code/pages are then integrated by engineers/dbas. who yes, will be handing the the validation proper on their end using my forms, etc. Client-side, in-page pre-validation of select fields & elements has been welcomed in the past. All i was trying to provide here.
Its totally acceptable for me at this point to demo for them the behaviour of firing the event.
To paraphrase my prototype:
Code:
<script type="text/javascript" src="../../js/jquery-1.3.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
//show
$("input#emailprimary").change(function() {
$("div#alertemailquestion").fadeIn("fast");
});
});
</script>
Code:
<p>Primary E-mail: <input type="text" id="emailprimary" value="current@email.com"></p>
<p>Alert E-mail: current@email.com</p>
<div id="alertemailquestion" style="display:none;">
<p>Update Alert E-mail to Primary E-mail?</p>
As always, thanks for the assistance & responses...
|
|
|
|
09-10-2009, 04:18 PM
|
Re: Using jquery to check value of text field, compare to string, fire event
|
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
HTML Code:
<p>Primary E-mail: <input type="text" id="emailprimary" value="current@email.com"></p>
<input type="hidden" id="currentemail" value="current@email.com">
<p>Alert E-mail: current@email.com</p>
<div id="alertemailquestion" style="display:none;">
<p>Update Alert E-mail to Primary E-mail?</p>
Code:
$(document).ready(function() {
$("input#emailprimary").blur(function() {
if(this.value == $("#currentemail").attr("value") {
alert("Email is the same as current!");
$("#alertemailquestion").show();
}
else {
alert("Email is not the same as current!");
$("#alertemailquestion").hide();
}
});
});
Note the new hidden field with the current email in the value attribute.
You may also want to look into how the JavaScript confirm() function works
Last edited by wayfarer07; 09-10-2009 at 04:22 PM..
|
|
|
|
|
« Reply to Using jquery to check value of text field, compare to string, fire event
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|