dongyao2129 2015-01-02 22:22
浏览 104
已采纳

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

I'm trying to show errors in real time on my registration form, instead of being redirected to another page (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. When a user submits this form it calls on register.PHP as an action. Inside register.php there is a line of code:

js_include('js/register.js');

This javascript checks that certain data is submitted correctly within registration.html. I'd like this check to be performed before submitting the form and causing it to redirect to register.php. I only need it to direct to register.php if javascript says everything is good.

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). 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.

Here is register.js, all feedback is welcome! 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;
    }
}
  • 写回答

1条回答 默认 最新

  • dongmi4809 2015-01-02 22:32
    关注

    Here is an example of how to test a form field using jQuery's blur() function -

    $('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>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致