开发者

Jquery Form Validation - Can't be empty OR a certain string?

开发者 https://www.devze.com 2023-03-07 12:16 出处:网络
I have a form that has a minimal available space on a website, so to keep it as small as possible I am ditching the labels and using the jquery plugin \'defaultvalue\' to fill the form fields with the

I have a form that has a minimal available space on a website, so to keep it as small as possible I am ditching the labels and using the jquery plugin 'defaultvalue' to fill the form fields with the required content - eg The field for the persons full name is filled with 'Please enter your name' when you load the page. On clicking into the form field it clears itself allowing you to type in your name.

Some of the fields need validating using the validate jquery plugin. Fields like the email one are fine, as the content needs to be an email address to validate.

Other fields need text content - but I can't work out how to validate it.

For example for the name field I need it to validate = IF field contents ARE blank OR "please enter your name" SHOW ERROR.

So it can't be blank, so the preset string.

How would you go about this using the jquery.validate plugin?

The form is on the top left of this homepage: http://www.mediplacements.com/

Some of the code also below. The current validating code is:

$("#fp-form").validate({
    rules: {
        fpfieldname: { required: true, minlength: 5 },
        fpfieldnumber: { required: true, minlength: 10 },
        fpfieldaddress: "required email",
        fpfieldprofession:  { required: true, minlength: 5 },
        fpfieldduration:  { required: true, minlength: 5 },
        cv:  { required: true }
    },

The form looks like this:

<form id="fp-form" action="" method="post" enctype="multipart/form-data">
            <input name="fpfieldname" id="fpfieldname" class="required" title="Your full name" /><br />
            <input name="fpfieldnumber" id="fpfieldnumber" class="required" title="Your phone number" /><br />
            <input name="fpfieldaddress" id="fpfieldaddress" class="required" title="Your email address" /><br />
            <inpu开发者_如何学JAVAt name="fpfieldprofession" id="fpfieldprofession" class="required" title="Your profession" /><br />
            <input name="fpfieldduration" id="fpfieldduration" class="required2" title="Temp or perm" /><br />
            <input name="cv" type="file" id="cv" value="" size="10"   />
            <input alt="Submit" name="Submit" class="send-btn" type="image" id="Submit" src="./img/submit-reg.gif" tabindex="5" />
         </form>  


using just jQuery is pretty straight forward

var value = $("#fpfieldduration").val();
if (value === "" || value == "please enter your name"){
  alert("ERROR");
}

http://jsfiddle.net/BJv8j/4/


you can use the required rule with callback to implement that behaviour

but, it's a duplicate of a recent question

0

精彩评论

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