Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery validation plugin, how can i validate a field but not have it required
    primarykey
    data
    text
    <p>I am using jquery validation plugin for clientside validation of a form. </p> <p>I have a field StandardPrice which is required and needs to be a number. The validation code below works perfect.</p> <p>I have another field in a form called MinPrice that is optional but I do want to validate that if a user puts something in there that it is a number. I thought by removing the <strong>required: true</strong> but still having the <strong>number: true</strong> it would support this but it seems like its not allowing me to leave this field blank (even though I don't have <strong>required: true</strong> set)</p> <p>Does jquery validation plugin support validating a field IF its populated but leaving it alone if its blank.</p> <p>Here is my code:</p> <pre><code> $("#productForm").validate({ rules: { StandardPrice: { required: true, number: true }, MinPrice: { number: true } } }); </code></pre> <p>Here is a screenshot of the validation when the field is empty:</p> <p><img src="https://i.stack.imgur.com/n443O.png" alt="enter image description here"></p> <p>and here is my html output (my actual code is using a lot of HTML.helpers() and Html.ValidationMessageFor()</p> <pre><code> &lt;form action="/Product/EditProduct" id="productForm" method="post"&gt; &lt;fieldset&gt; &lt;legend&gt;Product&lt;/legend&gt; &lt;div class="editor-label"&gt; &lt;label for="Product_Name"&gt;Name&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input class="required" id="Product_Name" name="Product.Name" style="width:450px;" type="text" value="Linzer Tart" /&gt; &lt;span class="field-validation-valid" data-valmsg-for="Product.Name" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;label for="Product_StandardPrice"&gt;StandardPrice&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input data-val="true" data-val-number="The field StandardPrice must be a number." data-val-required="The StandardPrice field is required." id="Product_StandardPrice" name="Product.StandardPrice" style="width:45px;text-align:right;" type="text" value="1.50" /&gt; &lt;span class="field-validation-valid" data-valmsg-for="Product.StandardPrice" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;label for="Product_MiniPrice"&gt;MiniPrice&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input data-val="true" data-val-number="The field MiniPrice must be a number." data-val-required="The MiniPrice field is required." id="Product_MiniPrice" name="Product.MiniPrice" style="width:45px;text-align:right;" type="text" value="0.00" /&gt; &lt;span class="field-validation-valid" data-valmsg-for="Product.MiniPrice" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Product_Id" name="Product.Id" type="hidden" value="102" /&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; </code></pre> <p></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.
    1. This table or related slice is empty.
    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