/    Sign up×
Community /Pin to ProfileBookmark

how to fire an event when input field has changed?

HI
How or what event handler should I use to execute function when value of the input element was changed…

Basically what i want to do is to chek if 2 password filed are identical if they are not make the input element backrgound red.
And I want to do this as the user working on the form rather then when they press the submit button

Any sudgestions?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJul 07.2006 — [code=html]<script type="text/javascript">
onload = function () {
var e, i = 0
while (e = document.forms[0].elements[i++]) {
if (e.type == 'password') e.onchange = function () {
if (this.form.password.value != this.form.confirm.value) {
this.form.password.className = 'error'
this.form.confirm.className = 'error'
} else {
this.form.password.className = ''
this.form.confirm.className = ''
}
}
}
}
</script>

<style type="text/css">
.error {background-color:#a00; color:#fff}
fieldset {width:20em}
label {display:block; text-align:right}
input {margin-left:1ex; width:10em}
button {display:block; margin:auto}
</style>

<form action="somes-script.pl">
<fieldset>
<label>Password<input name="password" type="password"><label>
<label>Confirm Password<input name="confirm" type="password"><label>
<button type="submit">Submit</button>
</fieldset>
</form>[/code]
Copy linkTweet thisAlerts:
@ananiasJul 07.2006 — ... if they are not make the input element backrgound red ...[/QUOTE]Charles code looks perfect and does exactly what you want, but I think enabling the submit button would be a better way to signal field validation.
Copy linkTweet thisAlerts:
@CharlesJul 07.2006 — Charles code looks perfect and does exactly what you want, but I think enabling the submit button would be a better way to signal field validation.[/QUOTE]Better to diasble it on error. Disable it by default and users without JavaScript will cry out to their God against you.
Copy linkTweet thisAlerts:
@alexusauthorJul 07.2006 — cool thanks!

how can I disable submit button? i dont see the style for this?
Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — Not a style...

[I]HTMLInputElement[/I].disabled = true;
Copy linkTweet thisAlerts:
@alexusauthorJul 07.2006 — when I added the script to my page im getting the error that bject does not support the value :-(

Is there any way to to the same but only with spesified element?
Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — Show the line of code getting the error.
Copy linkTweet thisAlerts:
@CharlesJul 07.2006 — Show the line of code getting the error.[/QUOTE]Better yet, give us the URL.
Copy linkTweet thisAlerts:
@alexusauthorJul 07.2006 — um... i got baanch oh php there too :-(
×

Success!

Help @alexus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...