douqiao2008 2014-05-04 00:14
浏览 32

使用表单验证覆盖所有基础,客户端/服务器 - 什么是最好的方法?

I know there are many methods of validating forms on both client and server side but I was wondering what was the best practice?

Currently, I have Javascript functions validating form input fields 'on the fly' with onkeyup/onblur functions like so:

(Partial code:)

<p class="form-registerUserName">
    <div class="upperLabel">
        <label for="registerUserName">User Name</label>
        <span class="required">*</span>
    </div>
    <input
        id="registerUserName"
        name="registerUserName"
        type="text"
        size="24"
        maxlength="24"
        value="<?php echo $_SESSION['registerUserName'];?>"
        onkeyup="validateName()"
        onblur="checkDuplicateName(); validateName()"
    >
    <label for="registerUserName" class="hint" id="registerUserNameHint"></label>
</p>

With Javascript functions like:

function validateName() {
    userName = document.getElementById("registerUserName").value.trim();
    re = /^[a-zA-Z0-9_]{1,30}$/;

    if (userName==="") {
        document.getElementById('registerUserName').style.borderColor="red";
        document.getElementById('registerUserNameHint').innerHTML = 'required';
    } else if (!re.test(userName)) {
        document.getElementById('registerUserName').style.borderColor="red";
        document.getElementById('registerUserNameHint').innerHTML = 'only alphanumeric characters and _';
    } else {
        document.getElementById("registerUserName").setAttribute("style","border-color: rgb(221,221,221) rgb(241,241,241) rgb(241,241,241) rgb(221,221,221);");
        document.getElementById('registerUserNameHint').innerHTML = '';
    }
} //validateName()

..So that the input box turns red and shows a hint on the side of the box if it does not validate.

So my question was - What is the best way to prevent the form from submission to my (Mysqli) database when the user hits submit?

(and second question..) Do I run an additional php server-side script after client-side validation has cleared?

Some ways I imagined to accomplish this is by having my Javascript functions set a Session variable that indicates an error condition, and not allow a submit if there was.

I am not certain how to do that, or how I set up my 'submit' to not work unless the error condition was cleared.

Would appreciate any help on that.

Then do I re-validate the same data (in the same manner) with php again, after a successful client-side validation before inserting into my database?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duanou2526 2014-05-04 00:18
    关注

    First off, always do server-side validation!

    Second, HTML5 form validation is well supported.
    Examples: http://html5pattern.com/

    You can then use CSS for validation styling.

    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来