js模糊验证表格

时间:2015-10-18 14:27:10

标签: javascript html validation

我正在尝试验证输入的值,但是使用此代码我没有收到消息(输入框变为红色):

document.forms.richiediAPE.your-email.addEventListener('blur', function(e) {
    if (!e.target.validity.valid) {
        e.target.setCustomValidity("Heyy");
    }
});

<!-- html -->
<form name="richiediAPE" action="mail.php">
    <p>
        <label>Name & Surname</label>
        <input class="name&surname" type="text" value="" name="your-name" maxlength="3" required/>
    </p>

    <p>
        <label>E-mail</label>
        <input class="email" type="email" value="" name="your-email" required/>
    </p>
    <input class="submit" type="submit" value="Request">
</form>

1 个答案:

答案 0 :(得分:1)

使用Javascript获取表单是另一种方式:

document.forms['form_name']
document.forms['form_name']['form_element_name']

使用:

document.forms['richiediAPE']['your-email'].addEventListener('blur',function(e){
     /* Yourself actions. */
     alert(document.forms['richiediAPE']['your-email'].value);
     /* This alerts the value of the text input. */
);

但你必须知道在document.forms中你得到一个表单和一个元素的名称(name tag,name =&#34;&#34;),当然?