您好,欢迎访问一九零五行业门户网

Javascript的表单验证长度_javascript技巧

javascript 可用来在数据被送往服务器前对 html 表单中的这些输入数据进行验证。
该采用什么样的方式对用户作出提醒呢?你一定不想用alert()提示框
在输入域后加一个sqan标签
function validate_length(inputfiled,helptext){//如果输入域内容是空,则在span标签内提醒if(inputfiled.value.length==0){if(helptext!=null)helptext.innerhtml=文本框不能为空;}//如果输入域不空,则清空span标签内的内容else if(helptext!=null)helptext.innerhtml=}
helptext是传入的span对象
用span标签来为用户作出提醒,不会像alert那样阻挡用户视觉
除了非空验证,还有尺寸问题
验证数据长度

这里的参数变成了四个,第一个是文本最小长度,第二个是文本最长长度
function validate_length(minlegth,maxlength,inputfiled,helptext){if(inputfiled.value.lengthmaxlength){if(helptext!=null){helptext.innerhtml=请输入长度为+minlenght+到+maxlength+的文本;return false;}}else if(helptext!=null){helptext.innerhtml=return true;}}
验证邮政编码
function validate_zipcode(inputfiled,helptext){if(inputfiled.value.length!=5){if(helptext!=null)helptext.innerhtml=邮政编码长度必须为5位;return false;}else if(isnan(inputfiled.value)){if(helptext!=null)helptext.innerhtml=邮政编码必须为数字;return false;}else if(helptext!=null){helptext.innerhtml=return true;}}
其它类似信息

推荐信息