开发者

Ignore default text in jquery validation plugin

I am using the jquery validation plugin (bassistance.de/jquery-plugins/jquery-plugin-validation/id) for a bit of client-side form validation. The plugin is working great but I cannot figure out how to force it to ignore some default text within a textarea.

<form action="#" id="landingForm">
<p>
<textarea id="askTexarea" class="required textarea" name="askTextarea" cols="7" rows="3">THIS IS THE DEFAULT TEXT</textarea>
</p>
<p>
<label for="askExpert">Pick an Expert:</label>
<select id="askExpert" class="required select" name="askExpert">
    <option></option>
    <option>CHOICE 1</option>
    <option>CHOICE 2</option>
    <option>CHOICE 3</option>
</select>
</p>
<p class="email">
<label for="askEmail">Enter your email address:</label>
<inpu开发者_JS百科t id="askEmail" class="required email" name="email" />
</p>
<p class="submit">
<input class="submit" type="submit" value="Ask a Question" />
</p>
</form>

You will notice in the first textarea there is default text. Does anyone know how to make the validation plugin ignore this text and consider it invalid if a user submits the form with this default text or if the textarea is empty?

Thanks in advance for you help!


You can add a custom method that checks for the default text:

$.validator.addMethod("defaulText", function(value, element) {
  return this.optional(element) || element[0].defaultText != value;
}, "Please replace the default text");

Didn't yet test this implementation - if element[0].defaultText doesn't give the right value, replace that with an inline string.


Just add this alongside the plugin code:

if ($('#askTextarea')[0].value=="THIS IS THE DEFAULT TEXT") // invalidate code here


As from what I read in your question, what you were trying to do with the "DEFAULT TEXT" was to display a placeholder - a prompt to the user to enter text, but not a value to be sent to the server.

In that case you are better served with an HTML5 placeholder attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜