Jquery Empty validation for text field

I have a text box in the form, when the user submits the form, he must check whether the user has filled it in, another check will contain a few minute characters that he must fill out.

here is my code that doesn't check

$('.textboxid').bind("submit", function() { if($(this).val() == "") { jQuery.error('Fill this field'); return false; } }); 
+6
source share
4 answers

Your code is not validated because you are attaching a submit event to a text field. You must use forms for this.

In addition to the answer of the sir, which will work fine. Provide him with an alternative. You can also use regular expression checks.

A nice example that adds error messages after a text box.

 $("#yourFormId").submit(function() { var inputVal= $("#yourTextBoxId").val(); var characterReg = /^([a-zA-Z0-9]{1,})$/; if(!characterReg.test(inputVal)) { $("#yourTextBoxId").after('<span class="error">Maximum 8 characters.</span>'); } }); 
+3
source

Try:

 $("#yourFormId").submit(function() { var textVal = $("#yourTextBoxId").val(); if(textVal == "") { alert('Fill this field'); return false; } });
$("#yourFormId").submit(function() { var textVal = $("#yourTextBoxId").val(); if(textVal == "") { alert('Fill this field'); return false; } }); 
+5
source
  $(function () { var textVal = $("#yourTextBoxId").val(); if (textVal == "") { $("#error").fadeIn(500).show(); } }); 

add a div where the error message should appear.

  <div id="error">Enter Value...</div> 
+2
source
+1
source

Source: https://habr.com/ru/post/910472/


All Articles