简体   繁体   English

如何在HTML表单上实时显示错误? (包含Javascript)

[英]How to display error in real-time on HTML form? (Javascript included)

I'm trying to show errors in real time on my registration form, instead of being redirected to another page (register.php). 我试图在我的注册表上实时显示错误,而不是重定向到另一个页面(register.php)。

The index page on my site has a sign-up link which opens a popup registration form (registration.HTML) in a new window. 我网站上的索引页面上有一个注册链接,该链接在新窗口中打开一个弹出注册表单(registration.HTML)。 When a user submits this form it calls on register.PHP as an action. 用户提交此表单时,它将调用register.PHP作为操作。 Inside register.php there is a line of code: 在register.php内部有一行代码:

js_include('js/register.js');

This javascript checks that certain data is submitted correctly within registration.html. 此javascript检查是否在registration.html中正确提交了某些数据。 I'd like this check to be performed before submitting the form and causing it to redirect to register.php. 我希望提交表单并将其重定向到register.php 之前执行此检查。 I only need it to direct to register.php if javascript says everything is good. 如果JavaScript表示一切正常,我只需要它直接指向register.php。

You can test this yourself here If you click "sign up" in the top-right corner, and type in gibberish as the email and press Enter, it will redirect to register.php and show the error at the bottom (if you scroll down). 您可以在此处进行自我测试如果单击右上角的“注册”,然后输入乱码作为电子邮件,然后按Enter,它将重定向到register.php并在底部显示错误(如果向下滚动)。 I'd like this error to be displayed on the registration form. 我希望此错误显示在注册表中。

I tried including the js below within some script tags on my html page, but it still redirects me. 我试图将下面的js包含在我的html页面的一些脚本标签中,但是它仍然可以重定向我。

Here is register.js, all feedback is welcome! 这是register.js,欢迎所有反馈! Thank you 谢谢

$(document).ready(function() {
    $('.formFieldWarning').hide();})

function checkRegisterFormSubmit() {
    $('.formFieldWarning').hide();
    var errors = 0;
    // Check the user name
    if($('#username').val() == '') {
        $('#username_warning1').show();
        errors++;
    } else {
        if ($('#username').val().length < 2 ) {
            $('#username_warning2').show();
            errors++;
        }
    }
    // Check the password
    if ($('#password').val().length < 2 ) {
        $('#password_warning1').show();
        errors++;
    } else {
        if ($('#password').val() == $('#username').val() ) {
            $('#password_warning2').show();
            errors++;
        }
    }
    // Check the password_verification
    if ($('#password_verification').val() != $('#password').val() ) {
        $('#password_verification_warning1').show();
        errors++;
    }
    // Check the email address
    if($('#email').val() == '') {
        $('#email_warning1').show();
        errors++;
    } else {
        if ($('#email').val().search(/^\w+((-|\.|\+)\w+)*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,63}$/) == -1) {
            $('#email_warning2').show();
            errors++;
        }
    }
    if (errors != 0) {
        $('#form_not_submit_top').show();
        $('#form_not_submit_bottom').show();
        return false;
    } else {
        return true;
    }
}

Here is an example of how to test a form field using jQuery's blur() function - 这是一个如何使用jQuery的blur()函数测试表单字段的示例-

 $('input[name="foo"]').blur(function() { var currentValue = $(this).val(); var testValue = 'crumple'; if(currentValue != testValue) { $(this).next('span').html('FAIL'); } else { $(this).next('span').html(''); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input name="foo" type="text" /><span></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM