网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

 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随机显示图片的简单示例
下一篇:php实现加减法验证码代码
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?