Home > Web Front-end > JS Tutorial > body text

A simple example of determining whether the value entered by the user is empty in js_javascript skills

WBOY
Release: 2016-05-16 17:07:40
Original
1042 people have browsed it

Determining whether the value entered by the user is empty in js is something that everyone uses a lot. There is nothing to write about. But I did it. The reason is just that I think my code is good. For your reference.

This is an excerpt from the user registration page in one of my projects. For most people, this is almost 100% experienced.
Post the code, these codes are all written in js. It’s not difficult, it’s easy to understand. When you read it, you just need to distinguish between the two js classes.
Front page code: reguser.aspx

Copy code The code is as follows:

<%@ Page language="c#" Codebehind="RegUser.aspx.cs" AutoEventWireup="false" Inherits="Enterprise.Web.RegUser" %>


   
        用户注册
       
       
       
       
       
       
        <script><br>            var reg    = {};<br>            reg.userName    = '';<br>            reg.password    = '';<br>            reg.confirmPassword    = '';<br>            reg.question    = '';<br>            reg.answer    = '';<br>            reg.url    = '';<br>            reg.sex    = 1;<br>            reg.email    = '';<br>            reg.tel    = '';<br>            reg.mobile    = '';<br>            reg.qq    = '';<br>            reg.address    = '';<br>            reg.postalcode    = '';<br>            reg.form    = null;<br><br>            function btnSubmit_onclick()<br>            {<br>                reg.form    = document.forms[0];<br>                var comFun    = new commonFunction();<br><br>                if(!comFun.checkIsEmpty(reg.form))<br>                {<br>                    return false;<br>                }<br><br>                if(comFun.$getElementById('txtPassword').value!=comFun.$getElementById('txtConfirmPassword').value)<br>                {<br>                    alert('两次密码输入不一致');<br>                    comFun.$getElementById('txtConfirmPassword').select();<br>                    return false;<br>                }<br><br>                reg.userName    = comFun.$getElementById('txtUserName');<br>                reg.password    = comFun.$getElementById('txtPassword');<br>                reg.question    = comFun.$getElementById('txtQuestion');<br>                reg.answer        = comFun.$getElementById('txtAnswer');<br>                reg.url            = comFun.$getElementById('txtUrl');<br>                reg.email        = comFun.$getElementById('txtEmail');<br>                reg.tel            = comFun.$getElementById('txtTel');<br>                reg.mobile        = comFun.$getElementById('txtMobile');<br>                reg.qq            = comFun.$getElementById('txtQQ');<br>                reg.address        = comFun.$getElementById('txtAddress');<br>                reg.postalcode    = comFun.$getElementById('txtPostalcode');<br><br>                var es    = comFun.$getElementsByName('sex');<br>                var eL    = es.length;<br>                for(var i=0; i<eL; i )<br>                {<br>                    var    e    = es[i];<br>                    if(e.checked)<br>                    {<br>                        reg.sex    = e.value;<br>                        break;<br>                    }<br>                }<br><br>                RegUser.Reg(reg.userName.value, reg.password.value, reg.question.value, reg.answer.value, reg.url.value, reg.sex.value, reg.email.value, reg.tel.value, reg.mobile.value, reg.qq.value, reg.address.value, reg.postalcode.value, callback_Reg);<br>            }<br><br>            function callback_Reg(res)<br>            {<br>                var rv    = res.value;<br>                if(rv)<br>                {<br>                    alert('注册成功!');<br>                    window.location.href='/Default.aspx';<br>                }<br>                else<br>                {<br>                    alert('有错误发生,注册失败!有可能是用户名或者域名被别人注册过了!');<br>                }<br>            }<br><br>            // 检测用户名的域名是否被其它用户注册过了<br>            function checkIsRegistered(obj, errorS, t)<br>            {<br>                var v    = obj.value;<br><br>                var rv    = RegUser.CheckIsRegistered(v, t).value;<br>                if(rv)<br>                {<br>                    alert(errorS);<br>                    obj.select();<br>                    return false;<br>                }<br>            }<br>        </script>
   
   
       

           
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
           
用户名:

                   
*
密码:

                   
*
确认密码:

                   
*
密码提示问题:

                   
*
密码问题答案:

                   
*
站点Url:

                   
*
性别:

                   
男  
Email:

                   
固定电话:

                   
*
移动电话:

                   
QQ:

                   
住址:

                   
邮编:

                   
操作:

                   
  

       

   


在上面的代码中,有包含了一个CommonFunction.js文件,下面这是他的内容:
复制代码 代码如下:

/***************************************************** *******
*
* Public js function
*
****************************** **********************************/
function commonFunction()
{
    // check value is null or empty
    this.checkIsEmpty    = function(obj)
    {
        var flag    = true;
        for(var i=0; i        {
            var e    = obj.item(i);
            if(e.isRequired)
            {   
                if(e.value=='')
                {
                    alert(e.errorSForEmpty);
                    e.focus();
                    flag    = false;
                    break;
                }
            }

            if(e.isValidate)
            {
                if(this.checkValidate(e)==false)
                {
                    alert(e.errorSForValidate);
                    e.select();
                    e.focus();
                    flag    = false;
                    break;
                }
            }
        }

        return flag;
    }

    // check value is validate
    this.checkValidate    = function(e)
    {
        var v    = e.value;
        if(v!='')
        {
            return this.checkReg(e.validatePattern, v);
        }
    }

    // regexp validate
    this.checkReg    = function(pattern, value)
    {
        pattern    = pattern.substring(1, pattern.length-1);
        var reg    = new RegExp(pattern);
        if(!reg.test(value))
        {
            return false;
        }
    }

    // return an Element By id object for what id.
    this.$getElementById    = function(id)
    {
        var e    = document.getElementById(id);

        if(e!='undefined')
        {
            return e;
        }

        return;
    }

    // return an Element By name object for what id.
    this.$getElementsByName    = function(id)
    {
        var e    = document.getElementsByName(id);

        if(e!='undefined')
        {
            return e;
        }

        return;
    }
}

贴一张效果图片:

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template