How to validate the form with jquery before the HTML 5 validation











up vote
1
down vote

favorite












I have a form in the Arabic language, i try to validate it with 2 ways: html 5 and j query also.
The problem is the English HTML 5 alert Appears in the first before that the arabic j query alert appears, but in my case i want the opposite with some change.
I want that the arabic j query alert appears in the first, and the HTML 5 English alert appears only if the j query can't stop the Cheating of user.. thanks in advance



<!DOCTYPE HTML>
<html>
<head>
<title>j query_validation</title>
<meta charset="UTF-8">
</head>

<body>
<form id="formId">
<input type="text" class="input-string" required/>
<input type="submit"/>
<p class="alert-error" style="display:
none">can't be empty</p>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script>
$('#formId').submit(function(e){
var inputValue = $('.input-string').val().length;
if(inputValue == 0)
{
$('.alert-error').show();
e.preventDefault();
}
});

</script>
</body>

</html>









share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a form in the Arabic language, i try to validate it with 2 ways: html 5 and j query also.
    The problem is the English HTML 5 alert Appears in the first before that the arabic j query alert appears, but in my case i want the opposite with some change.
    I want that the arabic j query alert appears in the first, and the HTML 5 English alert appears only if the j query can't stop the Cheating of user.. thanks in advance



    <!DOCTYPE HTML>
    <html>
    <head>
    <title>j query_validation</title>
    <meta charset="UTF-8">
    </head>

    <body>
    <form id="formId">
    <input type="text" class="input-string" required/>
    <input type="submit"/>
    <p class="alert-error" style="display:
    none">can't be empty</p>
    </form>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script>
    $('#formId').submit(function(e){
    var inputValue = $('.input-string').val().length;
    if(inputValue == 0)
    {
    $('.alert-error').show();
    e.preventDefault();
    }
    });

    </script>
    </body>

    </html>









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a form in the Arabic language, i try to validate it with 2 ways: html 5 and j query also.
      The problem is the English HTML 5 alert Appears in the first before that the arabic j query alert appears, but in my case i want the opposite with some change.
      I want that the arabic j query alert appears in the first, and the HTML 5 English alert appears only if the j query can't stop the Cheating of user.. thanks in advance



      <!DOCTYPE HTML>
      <html>
      <head>
      <title>j query_validation</title>
      <meta charset="UTF-8">
      </head>

      <body>
      <form id="formId">
      <input type="text" class="input-string" required/>
      <input type="submit"/>
      <p class="alert-error" style="display:
      none">can't be empty</p>
      </form>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
      <script>
      $('#formId').submit(function(e){
      var inputValue = $('.input-string').val().length;
      if(inputValue == 0)
      {
      $('.alert-error').show();
      e.preventDefault();
      }
      });

      </script>
      </body>

      </html>









      share|improve this question















      I have a form in the Arabic language, i try to validate it with 2 ways: html 5 and j query also.
      The problem is the English HTML 5 alert Appears in the first before that the arabic j query alert appears, but in my case i want the opposite with some change.
      I want that the arabic j query alert appears in the first, and the HTML 5 English alert appears only if the j query can't stop the Cheating of user.. thanks in advance



      <!DOCTYPE HTML>
      <html>
      <head>
      <title>j query_validation</title>
      <meta charset="UTF-8">
      </head>

      <body>
      <form id="formId">
      <input type="text" class="input-string" required/>
      <input type="submit"/>
      <p class="alert-error" style="display:
      none">can't be empty</p>
      </form>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
      <script>
      $('#formId').submit(function(e){
      var inputValue = $('.input-string').val().length;
      if(inputValue == 0)
      {
      $('.alert-error').show();
      e.preventDefault();
      }
      });

      </script>
      </body>

      </html>






      jquery html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 16:21









      Poul Bak

      5,42831132




      5,42831132










      asked Nov 22 at 15:18









      Nassim Nasri

      156




      156
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Just put e.preventDefault() at the start of your function.



          This prevents any default form submission behaviour. Just remember to actually submit the form if it's successful.



          $('#formId').submit(function(e){
          e.preventDefault();
          var inputValue;
          $('.input-string').each(function(){ // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each

          if(inputValue == 0){
          $('.alert-error').show();
          }
          else{
          $('.alert-error').hide(); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          form.submit();
          }

          }); // End .submit


          EDIT : Attached below is my the full answer, as per OP's request to fulfil further requirements in comments.






          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>








          share|improve this answer























          • I think you had a little problem with indentation here...
            – Jean-Marc Zimmer
            Nov 22 at 15:54










          • $('.input string') should be $('.input-string')
            – abdulsattar-alkhalaf
            Nov 22 at 15:55










          • @AboAlimk Thank you, good catch.
            – cmprogram
            Nov 22 at 15:58










          • thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
            – Nassim Nasri
            Nov 22 at 16:03






          • 1




            @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
            – Nassim Nasri
            Nov 22 at 16:34


















          up vote
          1
          down vote













          there are two errors in your js



           $('#formId').submit(function(e){
          var inputValue = $('.input string').val().length;
          if(inputValue == ''){
          $('.alert-error').show();
          e.preventDefault();
          }
          });


          length: you cant use int as string

          $('.input string'): should be $('.input-string'), you didn't add dash

          change it to



           $('#formId').submit(function(e){
          var inputValue = $('.input-string').val().length;
          if(inputValue === 0){
          $('.alert-error').show();
          e.preventDefault();
          }
          });





          share|improve this answer





















          • AboAlimk: thanks brother yes you're right
            – Nassim Nasri
            Nov 22 at 15:59











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433961%2fhow-to-validate-the-form-with-jquery-before-the-html-5-validation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Just put e.preventDefault() at the start of your function.



          This prevents any default form submission behaviour. Just remember to actually submit the form if it's successful.



          $('#formId').submit(function(e){
          e.preventDefault();
          var inputValue;
          $('.input-string').each(function(){ // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each

          if(inputValue == 0){
          $('.alert-error').show();
          }
          else{
          $('.alert-error').hide(); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          form.submit();
          }

          }); // End .submit


          EDIT : Attached below is my the full answer, as per OP's request to fulfil further requirements in comments.






          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>








          share|improve this answer























          • I think you had a little problem with indentation here...
            – Jean-Marc Zimmer
            Nov 22 at 15:54










          • $('.input string') should be $('.input-string')
            – abdulsattar-alkhalaf
            Nov 22 at 15:55










          • @AboAlimk Thank you, good catch.
            – cmprogram
            Nov 22 at 15:58










          • thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
            – Nassim Nasri
            Nov 22 at 16:03






          • 1




            @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
            – Nassim Nasri
            Nov 22 at 16:34















          up vote
          2
          down vote



          accepted










          Just put e.preventDefault() at the start of your function.



          This prevents any default form submission behaviour. Just remember to actually submit the form if it's successful.



          $('#formId').submit(function(e){
          e.preventDefault();
          var inputValue;
          $('.input-string').each(function(){ // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each

          if(inputValue == 0){
          $('.alert-error').show();
          }
          else{
          $('.alert-error').hide(); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          form.submit();
          }

          }); // End .submit


          EDIT : Attached below is my the full answer, as per OP's request to fulfil further requirements in comments.






          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>








          share|improve this answer























          • I think you had a little problem with indentation here...
            – Jean-Marc Zimmer
            Nov 22 at 15:54










          • $('.input string') should be $('.input-string')
            – abdulsattar-alkhalaf
            Nov 22 at 15:55










          • @AboAlimk Thank you, good catch.
            – cmprogram
            Nov 22 at 15:58










          • thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
            – Nassim Nasri
            Nov 22 at 16:03






          • 1




            @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
            – Nassim Nasri
            Nov 22 at 16:34













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Just put e.preventDefault() at the start of your function.



          This prevents any default form submission behaviour. Just remember to actually submit the form if it's successful.



          $('#formId').submit(function(e){
          e.preventDefault();
          var inputValue;
          $('.input-string').each(function(){ // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each

          if(inputValue == 0){
          $('.alert-error').show();
          }
          else{
          $('.alert-error').hide(); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          form.submit();
          }

          }); // End .submit


          EDIT : Attached below is my the full answer, as per OP's request to fulfil further requirements in comments.






          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>








          share|improve this answer














          Just put e.preventDefault() at the start of your function.



          This prevents any default form submission behaviour. Just remember to actually submit the form if it's successful.



          $('#formId').submit(function(e){
          e.preventDefault();
          var inputValue;
          $('.input-string').each(function(){ // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each

          if(inputValue == 0){
          $('.alert-error').show();
          }
          else{
          $('.alert-error').hide(); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          form.submit();
          }

          }); // End .submit


          EDIT : Attached below is my the full answer, as per OP's request to fulfil further requirements in comments.






          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>








          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>





          <!DOCTYPE HTML>
          <html>

          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <title>j query_validation</title>
          <meta charset="UTF-8">
          </head>

          <body>
          <form id="formId">
          <input type="text" class="input-string" required/>
          <button id="test-button" type="button" role="button">Test Me </button>
          <p class="alert-error" style="display:
          none">can't be empty</p>
          </form>

          <script>
          $('#test-button').click(function(e) {
          e.preventDefault();
          var inputValue = 0;
          $('.input-string').each(function() { // If you're checking for class, you may want to include this .each function, as many elements can have the same class. If you switch to ID, this can be removed.
          inputValue += $(this).val().length;
          }); // End .each
          // inputValue = 0;
          if (inputValue == 0) {
          $('.alert-error').each(function() {
          $(this).css("display", "block");
          });
          } else {
          $('.alert-error').each(function() {
          $(this).css("display", "none"); // Not really necessary if you're submitting and refreshing anyway, but it lets the user know it will be submitted successfully, before the page actually refreshed.
          });

          $("#formId").submit();
          }

          }); // End .submit
          </script>
          </body>

          </html>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 16:42

























          answered Nov 22 at 15:34









          cmprogram

          1,065519




          1,065519












          • I think you had a little problem with indentation here...
            – Jean-Marc Zimmer
            Nov 22 at 15:54










          • $('.input string') should be $('.input-string')
            – abdulsattar-alkhalaf
            Nov 22 at 15:55










          • @AboAlimk Thank you, good catch.
            – cmprogram
            Nov 22 at 15:58










          • thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
            – Nassim Nasri
            Nov 22 at 16:03






          • 1




            @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
            – Nassim Nasri
            Nov 22 at 16:34


















          • I think you had a little problem with indentation here...
            – Jean-Marc Zimmer
            Nov 22 at 15:54










          • $('.input string') should be $('.input-string')
            – abdulsattar-alkhalaf
            Nov 22 at 15:55










          • @AboAlimk Thank you, good catch.
            – cmprogram
            Nov 22 at 15:58










          • thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
            – Nassim Nasri
            Nov 22 at 16:03






          • 1




            @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
            – Nassim Nasri
            Nov 22 at 16:34
















          I think you had a little problem with indentation here...
          – Jean-Marc Zimmer
          Nov 22 at 15:54




          I think you had a little problem with indentation here...
          – Jean-Marc Zimmer
          Nov 22 at 15:54












          $('.input string') should be $('.input-string')
          – abdulsattar-alkhalaf
          Nov 22 at 15:55




          $('.input string') should be $('.input-string')
          – abdulsattar-alkhalaf
          Nov 22 at 15:55












          @AboAlimk Thank you, good catch.
          – cmprogram
          Nov 22 at 15:58




          @AboAlimk Thank you, good catch.
          – cmprogram
          Nov 22 at 15:58












          thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
          – Nassim Nasri
          Nov 22 at 16:03




          thanks for your help, but in your code if the user for example make the JavaScript disabled the the html 5 alert doesn't appears and stop the user – cmprogram
          – Nassim Nasri
          Nov 22 at 16:03




          1




          1




          @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
          – Nassim Nasri
          Nov 22 at 16:34




          @ cmprogram thanks a lot very helpful, i did it: $('button').click(function(e) { e.preventDefault(); var inputValue = $('.input-string').val().length; if(inputValue == 0) { $('.alert-error').show(); } });
          – Nassim Nasri
          Nov 22 at 16:34












          up vote
          1
          down vote













          there are two errors in your js



           $('#formId').submit(function(e){
          var inputValue = $('.input string').val().length;
          if(inputValue == ''){
          $('.alert-error').show();
          e.preventDefault();
          }
          });


          length: you cant use int as string

          $('.input string'): should be $('.input-string'), you didn't add dash

          change it to



           $('#formId').submit(function(e){
          var inputValue = $('.input-string').val().length;
          if(inputValue === 0){
          $('.alert-error').show();
          e.preventDefault();
          }
          });





          share|improve this answer





















          • AboAlimk: thanks brother yes you're right
            – Nassim Nasri
            Nov 22 at 15:59















          up vote
          1
          down vote













          there are two errors in your js



           $('#formId').submit(function(e){
          var inputValue = $('.input string').val().length;
          if(inputValue == ''){
          $('.alert-error').show();
          e.preventDefault();
          }
          });


          length: you cant use int as string

          $('.input string'): should be $('.input-string'), you didn't add dash

          change it to



           $('#formId').submit(function(e){
          var inputValue = $('.input-string').val().length;
          if(inputValue === 0){
          $('.alert-error').show();
          e.preventDefault();
          }
          });





          share|improve this answer





















          • AboAlimk: thanks brother yes you're right
            – Nassim Nasri
            Nov 22 at 15:59













          up vote
          1
          down vote










          up vote
          1
          down vote









          there are two errors in your js



           $('#formId').submit(function(e){
          var inputValue = $('.input string').val().length;
          if(inputValue == ''){
          $('.alert-error').show();
          e.preventDefault();
          }
          });


          length: you cant use int as string

          $('.input string'): should be $('.input-string'), you didn't add dash

          change it to



           $('#formId').submit(function(e){
          var inputValue = $('.input-string').val().length;
          if(inputValue === 0){
          $('.alert-error').show();
          e.preventDefault();
          }
          });





          share|improve this answer












          there are two errors in your js



           $('#formId').submit(function(e){
          var inputValue = $('.input string').val().length;
          if(inputValue == ''){
          $('.alert-error').show();
          e.preventDefault();
          }
          });


          length: you cant use int as string

          $('.input string'): should be $('.input-string'), you didn't add dash

          change it to



           $('#formId').submit(function(e){
          var inputValue = $('.input-string').val().length;
          if(inputValue === 0){
          $('.alert-error').show();
          e.preventDefault();
          }
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 15:53









          abdulsattar-alkhalaf

          31715




          31715












          • AboAlimk: thanks brother yes you're right
            – Nassim Nasri
            Nov 22 at 15:59


















          • AboAlimk: thanks brother yes you're right
            – Nassim Nasri
            Nov 22 at 15:59
















          AboAlimk: thanks brother yes you're right
          – Nassim Nasri
          Nov 22 at 15:59




          AboAlimk: thanks brother yes you're right
          – Nassim Nasri
          Nov 22 at 15:59


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433961%2fhow-to-validate-the-form-with-jquery-before-the-html-5-validation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu