Validate form before submitting to php

后端 未结 1 915
栀梦
栀梦 2021-01-15 19:35

Hello I\'m implementing a login page everything is working fine from the PHP side but now I would implement one more function using jQuery: I would like to check if the fiel

相关标签:
1条回答
  • 2021-01-15 20:36

    Bind an event to the submission of the form (Note: NOT the click of a submit button). That's where your code goes that checks the form, and if necessary, stops it from being posted.

    $('#loginForm').on('submit', function(e){
        if ($('#login').val()=='' || $('#password').val()==''){
            e.preventDefault();
            // alert('Fill in both fields');
            // You can use an alert or a dialog, but I'd go for a message on the page
            $('#errormsg').text('Fill in both fields').show();
        }else{
            // do nothing and let the form post
        }
    });
    

    If you prefer to show a message than use a dialog, somewhere on the page, preferably near the Submit button add the errormsg div

    <p id="errormsg" style="display:none;"></p>
    
    0 讨论(0)
提交回复
热议问题