PHP jQuery表单,带验证具体实现方法
程序员文章站
2022-06-09 12:46:37
email检测,密码重复检测,可以用于表单发送的各种环境,例如发送反馈,注册帐号
复制代码 代码如下:
<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>
<?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;
}
?>
...
email检测,密码重复检测,可以用于表单发送的各种环境,例如发送反馈,注册帐号
复制代码 代码如下:
<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;
}
?>