基于jquery,自己编写的基本验证

前言:感觉像小学生望莫吐槽,网上有一大堆的前端验证框架:jquery-validation、parsley  http://www.bootcdn.cn/parsley.js/

=================页面Demo===========================


 
 


   
   
 
 
 

 

         
  • *姓名必填信息

  •        
  • *年龄必填信息

  •        
  • *性别必填信息

  •        
  • *邮箱必填信息

  •        
  • *地址必填信息

  •        
  • 年级
              必选项
           

  •        

  •        
                否
                至少选择一项
           

  •        

  •         苹果
                香蕉
                梨
                至少选择一项
           

  •      

     
     

 

==========================js内容====基于jquery=======================================================

$(function(){
//错误提示样式
if (typeof($(".textError")) != "undefined") { 
$(".textError").css({"display":"none","font-size":"12px","font-family":"楷体","color":"#F00","margin-left":"10px"});
}
if (typeof($(".star")) != "undefined") { 
$(".star").css({"font-size":"12px","color":"#F00","margin-right":"5px"});
}
//输入框
if (typeof($(".textRequired")) != "undefined") {
$(".textRequired").blur(function(){
if($(this).val()==""){
$(this).next(".textError").show();
}else{
$(this).next(".textError").hide();
}
});
}
//下拉框
if (typeof($(".selectRequired")) != "undefined") {
$(".selectRequired").change(function(){
if($(this).val()==""){
$(this).next(".textError").show();
}else{
$(this).next(".textError").hide();
}
});
}
//单选按钮
if (typeof($(".radioRequired")) != "undefined") {
$(".radioRequired").click(function(){
$(".radioRequired").next(".textError").hide();
});
}
//多选按钮
if (typeof($(".checkboxRequired")) != "undefined") {
$(".checkboxRequired").click(function(){
if($(this).is(":checked")){
$(".checkboxRequired").next(".textError").hide();
}else{
checkboxRequired();
}
});
}
//form表单提交
if (typeof($(".myValidateForm")) != "undefined") {
$(".myValidateForm").submit(function(){
return myValid();
});
}
});
/**
* myValid()
*验证方法(用于自定义事件执行验证:单击事件等)
*/
function myValid(){
var flag = true;
if (typeof(flag) != "undefined") { 
  flag = inTextRequired();
}
if (typeof(flag) != "undefined") { 
  flag = inSelectRequired();
}
if (typeof(flag) != "undefined") { 
  flag = radioRequired();
}
if (typeof(flag) != "undefined") { 
  flag = checkboxRequired();
}
if(flag){
$(".textError").hide();
}
return flag;
}
/**
*输入框验证
*/
function inTextRequired(){
var flag = true;
$(".textRequired").each(function(index, element) {
        if($(element).val()==""){
$(element).next(".textError").show();
flag = false;
}
    });
return flag;
}


/**
*下拉框验证
*/
function inSelectRequired(){
var flag = true;
if($(".selectRequired").val()==""){
$(".selectRequired").next(".textError").show();
flag = false;
}
return flag;
}
/**
*单选按钮验证
*/
function radioRequired(){
var flag = false;
$(".radioRequired").each(function(index, element) {
        if($(element).is(":checked")){
flag = true;
}else{
$(".radioRequired").next(".textError").show();
}
    });
if(flag){
$(".radioRequired").next(".textError").hide();
}
return flag;
}


/**
*多选按钮验证
*/
function checkboxRequired(){
var flag = false;
$(".checkboxRequired").each(function(index, element) {
        if($(element).is(":checked")){
flag = true;
}else{
$(".checkboxRequired").next(".textError").show();
}
    });
if(flag){
$(".checkboxRequired").next(".textError").hide();
}
return flag;
}


/**
*验证手机号
*/
function isPhoneNum(str){
var regphone = /(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;//手机号码正则2016最新
var result = regphone.test(str);
return result;
}


/**
*验证邮箱
*/
function isEmail(str){
var regEmail = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/;//邮箱正则
var result = regEmail.test(str);
return result;
}


/**
*验证是否是数值(包含正负和小数点、零)
*/
function isNum(str){
var regNum = /^-?[1-9]*(\.\d*)?$|^-?0(\.\d*)?$/;
var result = regNum.test(str);
return result;
}


/**
*验证是否是整数
*/
function isInt(str){ 
var regInt = /^0$|^[1-9]\d*$/; //整数的正则表达式 
var result = regInt.test(str); 
return result; 





/**
*验证身份证号15位或18位
*/
function isCard(str){
var regCard = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
var result = regCard.test(str);
return result;
}

     

你可能感兴趣的:(基于jquery,自己编写的基本验证)