Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery.validate plugin - how to trim values before form validation
    primarykey
    data
    text
    <p>I'm using the excellent <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" rel="noreferrer">jquery.validation plugin</a> by Jörn Zaefferer and I was wondering whether there's a easy way to automatically trim form elements before they are validated?</p> <p>The following is a cut down but working example of a form which validates a email address:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $().ready(function() { $("#commentForm").validate({ rules: { email: { required: true, email: true } } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form class="cmxform" id="commentForm" method="get" action=""&gt; &lt;label for="cemail"&gt;E-Mail:&lt;/label&gt;&lt;input id="cemail" name="email" class="required email" /&gt; &lt;input class="submit" type="submit" value="Submit"/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The problem is that some users are getting confused because they accidently enter some whitespace in their email address, e.g. "test@test.com ". And the form won't submit and has a error message: "Please enter a valid email address.". Non-techy users don't know how to spot whitespace and may just quit the site rather than try to work out what they've done wrong.</p> <p>Anyway, I was hoping I could chain "<code>jQuery.trim(value)</code>" before the validation so the whitespace is removed and the validation error never occurs?</p> <p>I could use <a href="http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage" rel="noreferrer">addMethod</a> to build my own email validation function. But I'm sure there's a more elegant solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload