自制简单表单验证relative与absolute定位

简介:

html结构,用到了label与span

<label class="relative"><input type="text" name="name" id="name" class="colorblur" size="40" value="{sh:$info.name}"/><span class="msg_dialog"></span></label>

添加样式

复制代码
.relative{
    position: relative;
    font-weight: normal;

}
.msg_dialog{
    display: block;
    width:200px;
    height:auto;
    position: absolute;
    right:-200px;
    top:5px;
    color:red;
    overflow: hidden;
}
复制代码

表单添加checkForm()

<form action="{sh::U('User/addAgent')}" method="post" id="myform" onsubmit="return checkform();">
复制代码
function checkform(){
    if($("#name").val() == ''){
        showdialog($("#name"),"姓名不能为空");
        $("#name").focus();
        return false;
    }

    var email_val = $("#email").val();
    if(email_val == ''){
        showdialog($("#email"),"邮箱不能为空");
        $("#email").focus();
        return false;
    }
    reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if(!reg.test(email_val)){
        showdialog($("#email"),"邮箱不合法");
        $("#email").focus();
        return false;
    }

    // ajax email是否已存在
    if(ajaxCheck('email',email_val) == 1){
        showdialog($("#email"),"邮箱已存在,请勿重复添加");
        $("#email").focus();
        return false;
    }

    var tel_val = $("#tel").val();
    if(tel_val == '' || tel_val.length != '11'){
        showdialog($("#tel"),"手机不能为空或手机号有误");
        $("#tel").focus();
        return false;
    }

    // ajax 验证号码是否已存在
    if(ajaxCheck('tel',tel_val) == 1){
        showdialog($("#tel"),"号码已存在");
        $("#tel").focus();
        return false;
    }


    var reg = /^[0-9a-zA-Z]+$/;
    if($("#password").val() == '' || $("#password").val().length <6 || !reg.test($("#password").val())){
        showdialog($("#password"),"密码不能为空且必须为字符或数字");
        $("#password").focus();
        return false;
    }

    if($("#map_lng").val() == ''){
        showdialog($("#map_lng"),"请标注代理商位置");
        return false;
    }
}
复制代码

showdialog() 方法负责处理提示信息

function showdialog(obj,msg){
    obj.siblings(".msg_dialog").text(msg).show().delay(2000).hide(0);
}

delay(2000).hide(0) 延迟两秒后消失
ajax验证

复制代码
// ajax验证邮箱号码
function ajaxCheck(t,v){
    var res = '';
    $.ajax({
        tpye:"post",
        url:"{sh::U('User/ajax','todo=checkVal')}",
        data:"t="+t+"&v="+v,
        async: false,
        success:function(data){
            res = data;
        }
    });
    return res;
}
复制代码
复制代码
public function ajax(){
        $todo = $this->_request('todo','trim');
        switch ($todo) {
            case 'checkVal':
                $t = $this->_request('t','trim');
                $v = $this->_request('v','trim');
                if(empty($t) || empty($v)){
                    exit('0');
                }
                $agentModel = M('Agent');
                if($t == 'email'){
                    $count_email = $agentModel->where(array('email'=>$v))->count();
                    if($count_email > 0){
                        exit('1');
                    }
                }

                if($t == 'tel'){
                    $count_tel = $agentModel->where(array('tel'=>$v))->count();
                    if($count_tel > 0){
                        exit('1');
                    }
                }
                break;
            
            default:
                # code...
                break;
        }
    }
复制代码

效果

两秒后红色提示将消失





本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5009833.html,如需转载请自行联系原作者

相关文章
|
6月前
|
前端开发 容器
面试官:介绍一下CSS定位?absolute和relative分别依据什么定位?
面试官:介绍一下CSS定位?absolute和relative分别依据什么定位?
69 0
|
6月前
|
前端开发 容器
|
前端开发
CSS Position(定位)
CSS 有三种基本的定位机制:普通流、浮动和绝对定位。 除非专门指定,否则所有框都在普通流中定位。 也就是说,普通流中的元素的位置由元素在 HTML 中的位置决定。
132 0
CSS Position(定位)
html+css实战148-定位-相对relative
html+css实战148-定位-相对relative
103 0
html+css实战148-定位-相对relative
html+css实战150-定位-绝对定位
html+css实战150-定位-绝对定位
86 0
html+css实战150-定位-绝对定位
|
前端开发
CSS 文档中定位指南:static、relative、absolute、fixed、sticky
CSS 中 position 属性用于指定元素的定位方法的类型(static、relative、absolute、fixed、sticky)。
115 0
CSS 文档中定位指南:static、relative、absolute、fixed、sticky
|
前端开发 容器
web前端学习(二十三)——CSS3定位(position)、元素裁剪(clip)及鼠标样式(cursor)属性的相关设置
web前端学习(二十三)——CSS3定位(position)、元素裁剪(clip)及鼠标样式(cursor)属性的相关设置
web前端学习(二十三)——CSS3定位(position)、元素裁剪(clip)及鼠标样式(cursor)属性的相关设置
|
前端开发
前端~定位属性position(relative、absolute、fixed)的分析
前端~定位属性position(relative、absolute、fixed)的分析
176 0
|
Web App开发 前端开发 Android开发