views:

22

answers:

2

Hi,

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>
<input 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!

A: 

Just add this alongside the plugin code:

if ($('#askTextarea')[0].value=="THIS IS THE DEFAULT TEXT") // invalidate code here
xsznix
I keep trying this out but it seems to give a syntax error. Any other suggestions? Thanks for you help!
pixelflips
Could you post exactly what the error is?
xsznix
This is the js I have so far. Please let me know if you see any issues and where exactly I should put the line you suggested. Thanks again for all your help!$(document).ready(function () { $('#landingForm').validate({ rules: { askEmail: { required: true, email: true, }, askTextarea: { required: true, }, askExpert: { required: true }, }, });});
pixelflips
You know, you might as well just put this right before validation so the default text is invalidated:if ($('#askTextarea')[0].value=="THIS IS THE DEFAULT TEXT") $('#askTextarea')[0].value="";
xsznix
A: 

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.

Jörn Zaefferer