[jQuery] Create a custom rule

[jQuery] Create a custom rule


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi, I'm trying to create a new validation rule to check if a button has
a class.
I try to explain the scenario.
I have a button that makes an ajax call to check the availability of a
product.
When the app loads the button as class "to_check", when the button is
clicked an ajax call is fired, a server script return the availability
of the product and basing on this answer I change the class of the
button from "to_check" to "ok" or "ko".
Now I want to validate the button to se if the availability is being
checked and is ok, then I wrote this:
<pre>$.validator.addMethod('hasClass', function(value, element) { if($(element).hasClass(value)) {
return true;
} else {
return false
}
}, jQuery.format('Please check the button'));</pre>
Now I want to add this rule to the button with the metadata plugin
inside the class name with this syntax:
<pre id="line175"><span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);"><<span
class="start-tag">input</span><span class="attribute-name"> type</span>=<span
class="attribute-value">"button" </span><span class="attribute-name">name</span>=<span
class="attribute-value">"verifica_disponibilita" </span><span
class="attribute-name">
class</span>=<span class="attribute-value">"to_check {validate:{<b>hasClass:'ok'</b>, messages:{hasClass:'Verifica disponibilit&agrave;'}}}"
</span><span class="attribute-name">id</span>=<span
class="attribute-value">"verifica_disponibilita" </span><span
class="attribute-name">value</span>=<span class="attribute-value">"Verifica disponibilit&agrave;" </span><span
class="error"><span class="attribute-name">/</span></span>>
</span></pre>
<span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);">When I submit the form
it returns always false, I tried to log "value" and I get the button
value. </span>
<span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);"></span>
<span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);">How I can get the "ok"
passed via the metadata class? (bold one)</span>
<span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);"></span>
<span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);">Bye </span>
<span id="__firefox-tidy-id"
style="background-color: rgb(221, 221, 255);"></span>
<pre class="moz-signature" cols="72">--
gianiaz.net - web solutions
via piedo, 58 - 23020 tresivio (so) - italy
+39 347 7196482
</pre>
</body>
</html>