jQuery: Check whether val() has a specific value or not upon form submit
Posted
by Staffan Estberg
on Stack Overflow
See other posts from Stack Overflow
or by Staffan Estberg
Published on 2010-05-31T15:10:04Z
Indexed on
2010/05/31
15:13 UTC
Read the original article
Hit count: 275
Hi,
I'm trying to check whether val() has a specific value or not inside a form, if so deleting the contents / preventing the form being submitted depending on if both fields are filled in (stringOne and stringTwo). There's a default text for each input field, the first being "Namn, telefonnr, sökord" and the second "Område, plats, ort". If a user only fills in the first field the second must be cleared before the form string is passed on and vice versa. Like so -
// "This" refers to the form's submit button
if (($(this).siblings('input.stringOne').val("Namn, telefonnr, sökord")) && ($(this).siblings('input.stringTwo').val("Område, plats, ort"))) {
return false; // If nothing's filled in, then do not submit
}
else {
// If either field is not filled in, clear it
if ($(this).siblings('input.stringOne').val("Namn, telefonnr, sökord")) {
$(this).siblings('input.stringOne').val() == '';
}
if ($(this).siblings('input.stringTwo').val("Område, plats, ort")) {
$(this).siblings('input.stringTwo').val() == '';
}
}
jQuery version 1.2.6.
© Stack Overflow or respective owner