html form validation,html - JavaScript - Form Validation - Stack Overflow

这篇博客展示了如何利用jQuery简化JavaScript的基本操作,并实现了一个简单的表单验证功能。当DOM加载完成后,为带有'required'类的输入字段添加了失去焦点事件处理器。如果输入为空,将显示错误提示;否则,隐藏提示信息。
摘要由CSDN通过智能技术生成

As Kevmo mentioned a library, here is a quick and dirty example using jquery. Once you have the basics of javascript mastered look into jQuery, it will make your life a lot easier.

HTML not the absence on any calls to javascript functions and our required fields have the required class

Family name

Family Name Required

First name

First Name required

Dogs' name

CSS Just some basics

.requiredText {

display:none;

color:#F00;

}

label {

display:inline-block;

width:20%;

}

.required {

border-color:#F33;

}

input {

width:60%;

border:solid 1px #CCC;

margin-top:5px;

}

Javascript

I've used the following from jquery:

$(document).ready(function () { /* Execute when DOM is loaded */

/*Assign blur event handler to fields with required class */

/*I have used the id of the form (#formToValidate) to scope the selctor.

Not required but a good practice*/

$("#formToValidate .required").blur(function () {

if ($(this).val() === "") { /*Check the value of the item being blured"*/

$(this).next(".requiredText").slideDown('fast'); /* Slide down the nearest alert text sibling*/

} else {

$(this).next(".requiredText").slideUp('fast'); /* Slide up the nearest alert text sibling*/

}

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值