Pedro
Pedro

Reputation: 1477

Validation Form Replicating

I have a form that when is submitted it checks if the user selected the field, and if didn't it gives a message to fill, but if i keep clicking the message keeps duplicating, triplicating and goes on... How can i make the message appear only one time? Here is the code

$( "#myform" ).submit(function( event ) {
  var met = $("#mySelect").val();
  if (met === "0"){

    $( ".msn" ).append( "Select a Field" );
    return false 
  }else{
    $( "#myform" ).submit();
    }

});

Upvotes: 1

Views: 28

Answers (2)

cch
cch

Reputation: 3386

Since the problem is on the appended label try this:

$( "#myform" ).submit(function( event ) {
  var met = $("#mySelect").val();

  if (met === "0"){
    // Clear .msn 
    $(".msn").empty();

    // Append label
    $( ".msn" ).append( "Select a field" );

    return false 
  }else{
      $( "#myform" ).submit();
    }

});

Check this updated fiddle.

Upvotes: 1

underscore
underscore

Reputation: 6887

Use .one function

$( "#myform" ).one('submit', function( event ) {
  var met = $("#mySelect").val();
  if (met === "0"){

    $( ".msn" ).append( "Select a Field" );
    return false 
  }else{
    $( "#myform" ).submit();
    }

});

Upvotes: 0

Related Questions