Validform表单验证总结篇

Validform表单验证总结篇

Validform是一款基于jQuery的表单验证插件。它可以实现多种类型的表单验证,包括必填项验证、数字验证、邮箱验证、手机验证等等。本文将为大家提供Validform的完整攻略,详细讲解使用Validform进行表单验证的步骤和方法。

步骤1:下载Validform

首先,需要下载Validform插件。可以在官网(http://validform.rjboy.cn/)下载最新版本的Validform。下载完毕后,解压文件,将其中的jquery.validform.js和Validform_v5.3.2.css文件引入到HTML文档中。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Validform表单验证</title>
    <link rel="stylesheet" type="text/css" href="Validform_v5.3.2.css">
</head>
<body>
    <form action="#" method="post" id="myform">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username" class="demo"/>
        <br/>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" class="demo"/>
        <br/>
        <label for="email">邮箱:</label>
        <input type="text" name="email" id="email" class="demo"/>
        <br/>
        <label for="tel">手机号码:</label>
        <input type="text" name="tel" id="tel" class="demo"/>
        <br/>
        <input type="submit" value="提交">
    </form>

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.validform.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#myform").Validform({
                tiptype:2
            });
        });
    </script>
</body>
</html>

步骤2:为表单加上class

接下来,需要在表单中加上class。Validform是通过读取class来实现不同类型验证的。同类验证只需要添加一个相应的class即可,注意不要重复添加。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Validform表单验证</title>
    <link rel="stylesheet" type="text/css" href="Validform_v5.3.2.css">
</head>
<body>
    <form action="#" method="post" id="myform">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username" class="demo" datatype="*5-10"/>
        <br/>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" class="demo" datatype="*6-20"/>
        <br/>
        <label for="email">邮箱:</label>
        <input type="text" name="email" id="email" class="demo" datatype="e"/>
        <br/>
        <label for="tel">手机号码:</label>
        <input type="text" name="tel" id="tel" class="demo" datatype="m"/>
        <br/>
        <input type="submit" value="提交">
    </form>

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.validform.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#myform").Validform({
                tiptype:2
            });
        });
    </script>
</body>
</html>

在上面的示例代码中,我们为表单中的每一个input元素添加了一个datatype属性,用于指定验证类型。比如,datatype="*5-10"表示验证长度为5-10个字符;datatype="e"表示验证邮箱格式;datatype="m"表示验证手机号码格式等。

步骤3:初始化Validform

最后,需要调用Validform的初始化函数。在$(function())中添加$(selector).Validform(options)即可。options是一个参数对象,用来指定Validform的一些配置项。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Validform表单验证</title>
    <link rel="stylesheet" type="text/css" href="Validform_v5.3.2.css">
</head>
<body>
    <form action="#" method="post" id="myform">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username" class="demo" datatype="*5-10"/>
        <br/>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" class="demo" datatype="*6-20"/>
        <br/>
        <label for="email">邮箱:</label>
        <input type="text" name="email" id="email" class="demo" datatype="e"/>
        <br/>
        <label for="tel">手机号码:</label>
        <input type="text" name="tel" id="tel" class="demo" datatype="m"/>
        <br/>
        <input type="submit" value="提交">
    </form>

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.validform.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#myform").Validform({
                tiptype:2 //提示信息类型,可选值为1、2、3,默认为1
            });
        });
    </script>
</body>
</html>

在上面的示例代码中,我们将tiptype设置为2,表示提示信息以弹出层的形式提醒用户。

示例1:必填项验证

在表单中增加一个必填项验证,当用户未填写该项时,需要提示用户填写。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Validform表单验证</title>
    <link rel="stylesheet" type="text/css" href="Validform_v5.3.2.css">
    <style type="text/css">
        .error{color:red;margin-left:10px;}
    </style>
</head>
<body>
    <form action="#" method="post" id="myform">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username" class="demo" datatype="*5-10" nullmsg="请输入用户名"/>
        <span class="error">*</span>
        <br/>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" class="demo" datatype="*6-20" nullmsg="请输入密码"/>
        <span class="error">*</span>
        <br/>
        <label for="email">邮箱:</label>
        <input type="text" name="email" id="email" class="demo" datatype="e"/>
        <br/>
        <label for="tel">手机号码:</label>
        <input type="text" name="tel" id="tel" class="demo" datatype="m"/>
        <br/>
        <input type="submit" value="提交">
    </form>

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.validform.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#myform").Validform({
                tiptype:2, //提示信息类型,可选值为1、2、3,默认为1
                showAllError:true //是否显示全部错误信息,默认为false
            });
        });
    </script>
</body>
</html>

在上面的示例代码中,我们为用户名和密码增加了一个nullmsg属性,用于指定它们的必填提示信息。同时,我们还将showAllError设置为true,表示将会显示所有错误信息。

示例2:自定义验证规则

在表单中增加一个自定义的验证规则,用于验证输入内容必须为偶数。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Validform表单验证</title>
    <link rel="stylesheet" type="text/css" href="Validform_v5.3.2.css">
    <style type="text/css">
        .error{color:red;margin-left:10px;}
    </style>
</head>
<body>
    <form action="#" method="post" id="myform">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username" class="demo" datatype="*5-10" nullmsg="请输入用户名"/>
        <span class="error">*</span>
        <br/>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" class="demo" datatype="*6-20" nullmsg="请输入密码"/>
        <span class="error">*</span>
        <br/>
        <label for="email">邮箱:</label>
        <input type="text" name="email" id="email" class="demo" datatype="e"/>
        <br/>
        <label for="tel">手机号码:</label>
        <input type="text" name="tel" id="tel" class="demo" datatype="m"/>
        <br/>
        <label for="evennum">请输入偶数:</label>
        <input type="text" name="evennum" id="evennum" class="demo" datatype="evennum" errormsg="必须输入偶数"/>
        <span class="error">*</span>
        <br/>
        <input type="submit" value="提交">
    </form>

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.validform.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#myform").Validform({
                tiptype:2, //提示信息类型,可选值为1、2、3,默认为1
                showAllError:true, //是否显示全部错误信息,默认为false
                //自定义规则
                regcheck:{
                    evennum:function(gets,obj,curform,regxp){
                        //自定义验证规则,必须返回true或false
                        return parseInt(gets)%2==0; 
                    }
                }
            });
        });
    </script>
</body>
</html>

在上面的示例代码中,我们定义了一个名为evennum的验证规则,并将它添加到regcheck中。evennum验证规则用于检测输入内容是否为偶数,实际上是通过对输入内容进行求模运算判断是否为偶数。

阅读剩余 80%

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Validform表单验证总结篇 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • js 解析 JSON 数据简单示例

    下面我将详细讲解“js 解析 JSON 数据简单示例”的完整攻略: 什么是 JSON JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于传输和存储数据。它基于JavaScript语言的一个子集,具有良好的可读性,易于编写和理解。JSON格式数据可以简单地使用JavaScript对其进行解析和操作。下面是一个JSO…

    JavaScript 2023年5月27日
    00
  • IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理

    针对IE6/IE7中JavaScript json提示缺少标识符、字符串或数字的问题,可以采取以下处理方法: 方法一:手动添加JSON对象 对于IE6/IE7等低版本浏览器来说,没有原生的JSON对象,需要手动添加JSON对象。我们可以使用下面的代码来添加JSON对象: if (!window.JSON) { window.JSON = { parse: f…

    JavaScript 2023年5月27日
    00
  • 正则表达式中test、exec、match的区别介绍及括号的用法

    正则表达式是用于匹配文本的强大工具,它允许您使用模式来搜索、替换和操作文本。在使用正则表达式时,test、exec、match及括号都是常用的概念,它们的用法与意义都不同。 test、exec、match的区别介绍 test方法 test方法是RegExp对象的方法之一,它的作用是测试一个字符串是否匹配某个正则表达式,返回布尔值。如果匹配成功,test方法返…

    JavaScript 2023年6月10日
    00
  • javascript常用经典算法实例详解

    JavaScript常用经典算法实例详解 JavaScript常用经典算法实例涵盖了排序、查找、字符串处理等多个算法类型。下面将对其中的两个示例进行详细说明。 示例一:冒泡排序算法 冒泡排序是一种常见的排序算法,其基本思想是不断比较相邻两个元素的大小,将较大的元素向后移动,最终实现数组的升序排列。 下面是一个使用JavaScript实现冒泡排序的示例代码: …

    JavaScript 2023年5月18日
    00
  • 在JS中如何把毫秒转换成规定的日期时间格式实例

    为了将毫秒转化为规定格式的日期时间,我们需要使用JavaScript中内置的Date对象以及Date对象自带的各种方法,下面是具体的攻略。 一、使用Date对象的toLocaleString()方法 最简单的将毫秒转化为规定格式的日期时间的方法是使用Date对象的toLocaleString()方法。这个方法可以根据用户的本地时间格式自动将时间转化为字符串。…

    JavaScript 2023年5月27日
    00
  • 解决微信二次分享不显示摘要和图片的问题

    让微信二次分享能够正确显示摘要和图片,需要在网页head部分添加相关的meta标签。以下是具体的步骤: 在head部分添加以下meta标签: <meta property="og:title" content="网页标题"/> <meta property="og:description&q…

    JavaScript 2023年6月11日
    00
  • 学习JavaScript正则表达式

    学习JavaScript正则表达式可以分为以下几个步骤: 1. 了解正则表达式的基础概念 正则表达式是用来匹配文本模式的工具,它本身是一个字符串,并且可以包含特殊字符和普通字符。在学习正则表达式之前,需要了解一些基础概念,例如: 元字符:表示特殊含义的字符,例如\d表示数字,\w表示字母、数字或下划线。 字符集:用 [] 包含起来的字符集合,例如[abcd]…

    JavaScript 2023年6月10日
    00
  • JS中如何轻松遍历对象属性的方式总结

    当我们需要遍历 JavaScript 对象的属性时,我们可以使用 for…in 循环、Object.keys() 方法或 Object.getOwnPropertyNames() 方法。 for…in 循环遍历对象属性 for…in 循环会遍历对象的所有可枚举属性,包括原型链上的属性。对于每个属性,for…in 循环会执行一次循环体中的代码。…

    JavaScript 2023年5月27日
    00
合作推广
合作推广
分享本页
返回顶部