PHP jQuery表单带验证的具体实现方法

发布时间:2021-09-18 11:33:48 作者:chen
来源:亿速云 阅读:139

这篇文章主要介绍“PHP jQuery表单带验证的具体实现方法”,在日常操作中,相信很多人在PHP jQuery表单带验证的具体实现方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PHP jQuery表单带验证的具体实现方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

 Email检测,密码重复检测,可以用于表单发送的各种环境,例如发送反馈,注册帐号

PHP jQuery表单带验证的具体实现方法

复制代码 代码如下:


<div id="container"> 
        <?if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEmail($_POST['email']) || !validatePasswords($_POST['pass1'], $_POST['pass2']) || !validateMessage($_POST['message']) ) ):?> 
                <div id="error"> 
                    <ul> 
                        <?if(!validateName($_POST['name'])):?> 
                            <li><strong>Invalid Name:</strong> We want names with more than 3 letters!</li> 
                        <?endif?> 
                        <?if(!validateEmail($_POST['email'])):?> 
                            <li><strong>Invalid E-mail:</strong> Stop cowboy! Type a valid e-mail please :P</li> 
                        <?endif?> 
                        <?if(!validatePasswords($_POST['pass1'], $_POST['pass2'])):?> 
                            <li><strong>Passwords are invalid:</strong> Passwords doesn't match or are invalid!</li> 
                        <?endif?> 
                        <?if(!validateMessage($_POST['message'])):?> 
                            <li><strong>Ivalid message:</strong> Type a message with at least with 10 letters</li> 
                        <?endif?> 
                    </ul> 
                </div> 
            <?elseif(isset($_POST['send'])):?> 
                <div id="error" class="valid"> 
                    <ul> 
                        <li><strong>Congratulations!</strong> All fields are OK ;)</li> 
                    </ul> 
                </div> 
        <?endif?> 

        <form method="post" id="customForm" action=""> 
            <div> 
                <label for="name">Name</label> 
                <input id="name" name="name" type="text" /> 
                <span id="nameInfo">What's your name?</span> 
            </div> 
            <div> 
                <label for="email">E-mail</label> 
                <input id="email" name="email" type="text" /> 
                <span id="emailInfo">Valid E-mail please, you will need it to log in!</span> 
            </div> 
            <div> 
                <label for="pass1">Password</label> 
                <input id="pass1" name="pass1" type="password" /> 
                <span id="pass1Info">At least 5 characters: letters, numbers and '_'</span> 
            </div> 
            <div> 
                <label for="pass2">Confirm Password</label> 
                <input id="pass2" name="pass2" type="password" /> 
                <span id="pass2Info">Confirm password</span> 
            </div> 
            <div> 
                <label for="message">Message</label> 
                <textarea id="message" name="message" cols="" rows=""></textarea> 
            </div> 
            <div> 
                <input id="send" name="send" type="submit" value="Send" /> 
            </div> 
        </form> 
    </div> 

validation.php

复制代码 代码如下:


<?php 
    function validateName($name){ 
        //if it's NOT valid 
        if(strlen($name) < 4) 
            return false; 
        //if it's valid 
        else 
            return true; 
    } 
    function validateEmail($email){ 
        return ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email); 
    } 
    function validatePasswords($pass1, $pass2) { 
        //if DOESN'T MATCH 
        if(strpos($pass1, ' ') !== false)
            return false;
        //if are valid
        return $pass1 == $pass2 && strlen($pass1) > 5;
    }
    function validateMessage($message){
        //if it's NOT valid 
        if(strlen($message) < 10) 
            return false; 
        //if it's valid 
        else 
            return true; 
    } 
?> 

到此,关于“PHP jQuery表单带验证的具体实现方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. PHP从入门到高级需要掌握什么
  2. PHP表单验证的具体实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php jquery

上一篇:网站运营策略的示例分析

下一篇:如何更好提高网站用户体验度

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》