0

Hi, I am new to jQuery.

I want a validation that works like this:

If a text field has a specific value, say "Robi", then on submit click event the text field will become empty but any value other than "Robi" will submit the data successfully.

Some suggestions please.

0

3 Answers 3

1
$("form").on("submit", function() {
    return $.trim($("#myInputField").val(function(i, val) {
        return val == "Robi" ? "" : val;
    }).val()) != "";
});​

DEMO: http://jsfiddle.net/2mR8b/

Sign up to request clarification or add additional context in comments.

3 Comments

how can i target a specific text field if i have more than one textfield
If you want to target a specific text field I think you would use $('#id_of_textfield') instead of $(":text")
@user1496872 as Kenny mentioned, use IDs and make selection with hash # symbol. You can try the DEMO in the updated answer.
1

HTML:

<form id="myform">
<input type="text" id="txtfield" value="" />
</form>

JS:

$(document).ready(function() {

   $('#myform').submit(function() {
      if ($('#txtfield').val() == "Robi") { $('txtfield').val(''); return false; }
   });

});

Comments

1

HTML:

<form>
  <input type='text' id='name' />
</form> 

Script:

  $("form").on("submit", function() {
       var name = $("#name").val();
       if(name == "Robi"){
           $("#name").val("");
           return false;
        }
       else{
            // continue to submit
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.