HTML5表单必填属性。 设置自定义验证消息?

本文介绍如何在HTML5表单中使用oninvalid事件自定义验证消息,包括设置必填字段的错误提示,以及如何根据不同字段类型显示特定错误信息。

本文翻译自:HTML5 form required attribute. Set custom validation message?

I've got the following HTML5 form: http://jsfiddle.net/nfgfP/ 我有以下HTML5表单: http : //jsfiddle.net/nfgfP/

 <form id="form" onsubmit="return(login())"> <input name="username" placeholder="Username" required /> <input name="pass" type="password" placeholder="Password" required/> <br/>Remember me: <input type="checkbox" name="remember" value="true" /><br/> <input type="submit" name="submit" value="Log In"/> 

Currently when I hit enter when they're both blank, a popup box appears saying "Please fill out this field". 目前,当我将两个都空白时按Enter键时,会出现一个弹出框,提示“请填写此字段”。 How would I change that default message to "This field cannot be left blank"? 如何将默认消息更改为“此字段不能为空”?

EDIT: Also note that the type password field's error message is simply ***** . 编辑:还请注意,类型密码字段的错误消息只是***** To recreate this give the username a value and hit submit. 要重新创建此名称,请为用户名指定一个值,然后单击“提交”。

EDIT : I'm using Chrome 10 for testing. 编辑 :我正在使用Chrome 10进行测试。 Please do the same 请同样


#1楼

参考:https://stackoom.com/question/m7Bf/HTML-表单必填属性-设置自定义验证消息


#2楼

It's very simple to control custom messages with the help of the HTML5 oninvalid event HTML5 oninvalid事件的帮助下控制自定义消息非常简单

Here is the code: 这是代码:

User ID 
<input id="UserID"  type="text" required 
       oninvalid="this.setCustomValidity('User ID is a must')">

#3楼

It's very simple to control custom messages with the help of HTML5 event oninvalid HTML5事件oninvalid的帮助下控制自定义消息非常简单

Here is code: 这是代码:

<input id="UserID"  type="text" required="required"
       oninvalid="this.setCustomValidity('Witinnovation')"
       onvalid="this.setCustomValidity('')">

This is most important: 这是最重要的:

onvalid="this.setCustomValidity('')"

#4楼

The easiest and cleanest way I've found is to use a data attribute to store your custom error. 我发现的最简单,最干净的方法是使用数据属性存储您的自定义错误。 Test the node for validity and handle the error by using some custom html. 测试节点的有效性,并使用一些自定义html处理错误。 在此处输入图片说明

le javascript javascript

if(node.validity.patternMismatch)
        {
            message = node.dataset.patternError;
        }

and some super HTML5 和一些超级HTML5

<input type="text" id="city" name="city" data-pattern-error="Please use only letters for your city." pattern="[A-z ']*" required>

#5楼

Here is the code to handle Custom Error Message in HTML5 这是处理HTML5中的自定义错误消息的代码

<input type="text" id="username" required placeholder="Enter Name"
    oninvalid="this.setCustomValidity('Enter User Name Here')"
    oninput="this.setCustomValidity('')"  />

This part is important because it hides the error message when the user inputs new data: 这部分很重要,因为它在用户输入新数据时隐藏了错误消息:

oninput="this.setCustomValidity('')"

#6楼

Try this one, its better and tested: 试试这个,它更好并经过测试:

HTML: HTML:

<form id="myform">
    <input id="email" 
           oninvalid="InvalidMsg(this);" 
           oninput="InvalidMsg(this);"
           name="email"  
           type="email" 
           required="required" />
    <input type="submit" />
</form>

JAVASCRIPT: JAVASCRIPT:

function InvalidMsg(textbox) {
    if (textbox.value === '') {
        textbox.setCustomValidity('Required email address');
    } else if (textbox.validity.typeMismatch){
        textbox.setCustomValidity('please enter a valid email address');
    } else {
       textbox.setCustomValidity('');
    }

    return true;
}

Demo: 演示:

http://jsfiddle.net/patelriki13/Sqq8e/ http://jsfiddle.net/patelriki13/Sqq8e/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值