I'm using jquery validate plugin to validate an email address against a database using the remote method of this plugin.
I need to check the email address is not in our database already. Originally, the script in combination with server side code simply checked for the email address in our database and returned true or false, this worked successfully.
I now need to add an additional feature as it's now possible for us to have the same email address in the database twice as long as the siteid for the record is different i.e. if a record for [email protected] with siteid=site1 exists in the database, you can't add [email protected] with siteid=site1 again, but you can add [email protected] with siteid=site2.
I've confirmed that the serverside code is working and for a given email address and siteid, it returns true or false as expected.
I want to revalidate the email field whenever the siteid select box is changed. At the moment it seems to remember it's previous validation (it's already validated at load) and is not affected by the siteid change, unless I change the email from [email protected] to something else, then change it back, then it validates correctly for the new siteid. I've tried a combination of remove rules and add rules, nothing seems to work.
Help!
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
var enquiryform = {
rules: {
email: {
required: true,
email: true,
remote: 'emailcheck2.asp?siteid=' + $('#siteid').val()
}
},
messages: {
email: {
remote: 'exists already!'
}
}
};
var validatedform = $(".validatedform").validate(enquiryform).form();
$("#siteid").bind("change", function(e){
enquiryform.rules.email.remote='emailcheck2.asp?siteid=' + $('#siteid').val();
});
});
</script>
</head>
<body class="contactadd">
</head>
<body>
<form method="Post" class="validatedform">
<input class="email" id="email" name="email" type="text">
<select id="siteid" name="siteid">
<option value="1">Site 1</option>
<option value="2">Site 2</option>
</select>
<input id="submitbutton" type="submit" value="Create">
</form>
</body>
</html>