A simple instance in JS that determines whether the value entered by the user is empty

Source: Internet
Author: User
Tags reset zip

  This article is mainly in JS to determine whether the value of the user input is empty simple examples of the introduction, the need for friends can come to the reference, I hope to help you.

In JS to determine whether the user entered the value is empty, which is very much used by everyone. There's nothing to write about. And I wrote it. The reason is just self-righteous think that my code is well written, for your reference.   This is an excerpt from the user registration page in my project. For most people, this is almost 100% experienced. Paste code, the code is written in JS. It's not difficult, it's easy to read. Look at the time, as long as the difference two JS class on the line. Foreground page code: reguser.aspx     Code as follows: <%@ page language= "C #" codebehind= "RegUser.aspx.cs" autoeventwireup= "false" inherits= "Enterprise.Web.RegUser"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <html>     <head>     &NB Sp   <title> User Registration </title>         <meta name= "generator content=" Microsoft Visual Studio. NET 7.1 ">         <meta name=" Code_language "content=" C # ">       &nbsp ; <meta name= "vs_defaultClientScript" content= "JavaScript" >         <meta name= "Vs_ targetschema "content=" http://schemas.microsoft.com/intellisense/ie5 ">         <link rel=" Stylesheet "type=" Text/css "href="/css/enterprisewebsite.css ">         <script src= "/js/commonfunction.js" ></script>         <SC ript>             var reg    = {};             reg.username    = ';             Reg.password    = ';             Reg.confirmpassword    = ';             reg.question    = ';             reg.answer    = ';             reg.url    = ';             reg.sex    = 1;             reg.email    = ';             Reg.tel    = ';             reg.mobile    = ';             REG.QQ    = ';   &nbsp         reg.address    = ';             reg.postalcode    = ';             reg.form    = null;               function Btnsubmit_onclick ()             {                 reg.form    = document.forms[0];                 var comfun    = New commonfunction ();                   if (!comfun.checkisempty (reg.form))       &NB Sp         {                    return false;   & nbsp                               if (comfun. $getE Lementbyid (' Txtpassword '). Value!=comfun. $getElementById (' Txtconfirmpassword '). Value) &nbsp               {                    A Lert (' two times password input inconsistency ');                     Comfun $getElementById (' Txtconfirmpassword '). Select ();                     return false;                                   R Eg.username    = comfun. $getElementById (' txtUserName ');                 Reg.password    = comfun $getElementById (' Txtpassword ') ;                 reg.question    = comfun $getElementById (' txtquestion ') ;                 reg.answer        = comfun. $getElementById (' TX Tanswer ');                 Reg.url            = comfun. $getElementById (' Txturl ');                 reg.email        = comfun. $getElementById (' txt Email ');                 Reg.tel            = Comfun. $getElem Entbyid (' Txttel ');                 reg.mobile        = comfun. $getElementById (' TX Tmobile ');                 REG.QQ            = Comfun. $getEleme Ntbyid (' txtqq ');                 reg.address        = comfun. $getElementById (' t Xtaddress ');                 reg.postalcode    = comfun $getElementById (' Txtpostalco De ');                   var es    = comfun. $getElementsByName (' sex ');   &NBSP             var eL    = Es.length;                 for (var i=0 i<el; i++)           &NBS P     (                    var    e    = es [i];                     if (e.checked)           &NB Sp         {                        Reg.sex    = E.value;                         break;                                   & nbsp }                   Reguser.reg (Reg.userName.value, Reg.password.value, REG.Q Uestion.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);                           function Callback_reg (res) &N Bsp           {                var rv    = Res.val Ue                 if (RV)                 {& nbsp                   alert (' Registration successful! ');                     window.location.href= '/default.aspx ';                                 Else &NB Sp               {                    A Lert (' Error occurred, registration failed! ' may be a username or domain name has been registered by someone else! ');     &NBSP           {                         //Detect whether the domain name of the username has been registered by another user             function checkisregistered (obj, ErrorS, T)             {                var v    = Obj.val Ue                   var rv    = reguser.checkisregistered (V, t). value ;                 if (RV)                 {& nbsp                   alert (ErrorS);                     obj.select ();                     return false;                            }         </script>     </head>     <body ms_positioning= "GridLayout" >       & nbsp <form id= "Form1" method= "POST" runat= "Server" >             <table width= "98%" Borde R= "0" align= "center" cellpadding= "3" cellspacing= "0" >                 <TR&GT ;                     <TD width= "16%" ><div align= "right" > Username: & lt;/div>                     </td>                     <TD width= "84%" ><input type= "text" id= "txtUserName" onblur= " Checkisregistered (this, ' the username has been registered, please use the other! ', 1) ' maxlength= ' isrequired= ' true ' isvalidate= ' true ' errorsforempty= ' User name cannot be empty! "errorsforvalidate=" Match Error! begins with a letter, allowing 3-16 bytes, allowing alphanumeric underlines and licensed security symbols! "validatepattern="/^[a-za-z][a-za-z0-9_$%]{ 2,15}$/"title=" user name must be filled in! " ><font Color= "#ff0000" >*</font></td>                 </tr>                 <tr>                   &NBSP ; <td><div align= "Right" > Password:</div>                     & lt;/td>                     <td><input type= "password" id= "t Xtpassword "Maxlength=" isrequired= "true" isvalidate= "true" errorsforempty= password cannot be empty! "errorsforvalidate=" Match error! Requires 3-16 bytes, allowing alphanumeric underlines and licensed security symbols!! "validatepattern="/^[a-za-z0-9_$%]{2,15}$/"title=" Password must be filled in! " ><font color= "#ff0000" >*</font></td>                 </ tr>                 <tr>                     <td><div align= "right" > Confirm password: </div>                     </td>                     <td><input type= "password" id= "Txtconfirmpassword" isrequired= "true" errorsforempty= "Confirm password cannot be empty!" ><font color= "#ff0000" >*</font></td>                 </ tr>                 <tr>                     <td><div align= "right" > Password hint problem:</div>             &NB Sp       </td>                     <td><input Type= "text" id= "txtquestion" maxlength= "isrequired=" true "isvalidate=" true "errorsforempty=" password hint question not filled in! " Errorsforvalidate= "Length must be between 8-50 words and no spaces!" validatepattern= "/s{8,50}/" title= "password hint problem must be filled in!" ><font color= "#ff0000" >*</font></td>                 </tr>             &NBSP ;   <tr>                     <td><div align= "right" ; Password problem answer:</div>                     </td>     &NBSP ;               <td><input type= "text" id= "Txtanswer" maxlength= "isrequired" = ' true ' isvalidate= ' true ' errorsforempty= ' the answer to the password question is not filled in! "errorsforvalidate=" length must be between 8-50 words and no space! "validatepattern="/ s{8,50}/"title=" Password question answer must fill in! ><font color= "#ff0000" >*</font></td>                 </ tr>                 <tr>                     <td><div align= "right" > site url:</div>             &NBS P      </td>                     <td><input Type= "text" id= "Txturl" onblur= "checkisregistered (this, ' The URL has been registered, please use the other! ', 2)" Maxlength= "isrequired=" true " Isvalidate= "true" errorsforempty= site URL cannot be empty! "errorsforvalidate=" Site URL is not well-formed! "validatepattern="/^[a-za-z0-9]{1,20} $/"title=" Site URL must be filled in! " ><font color= "#ff0000" >*</font></td>                 </ tr>                 <tr>                     <td><div align= "right" > Gender:</div>                     </td>                     <td><input Type= "Radio" id= "Boy" name= "Sex" value= "1" checked> male  <input type= "Radio" id= "Girl" name= "Sex" value= "0" > Women </td>   &NBSp             </tr>                 <tr> & nbsp                   <td><div align= "right" >Email:</div> &nbs P                   </td>             &NBSP ;       <td><input type= "text" id= "Txtemail" isvalidate= "true" errorsforvalidate= "email format is not correct!" Validatepattern= "/^.+@" ([?) [a-za-z0-9-.] +. ([a-za-z]{2,3}| [0-9] {1,3}) (]?) $/"></td>                 </tr>                 <tr>                     <td><div al Ign= "Right" > Fixed telephone:</div>                     </td>                    ; td><input type= "text" id= "Txttel" isrequired= "true" isvalidate= "true" errorsforempty= "fixed telephone cannot be empty!" errorsforvalidate= "Fixed phone format is not correct!" Please use the 0592-5555555 format! "validatepattern="/^ (d{3}-|d{4}-)? (d{8}|d{7}) $/"title=" fixed phone must be filled in! " ><font color= "#ff0000" >*</font></td>                 </ tr>                 <tr>                     <td><div align= "right" > Mobile:</div>             &NBSP ;       </td>                     <td><input Type= "text" id= "Txtmobile" isvalidate= "true" errorsforvalidate= "mobile phone format is not correct!" validatepattern= "/^1d{10}$/" ></ td>                 </tr>             &NBSP ;   <tr>           &NBSP         <td><div align= "right" >QQ:</div>             & nbsp       </td>                     <td><input Type= "text" id= "TXTQQ" isvalidate= "true" errorsforvalidate= "QQ format is not correct!" validatepattern= "/^[1-9]*[1-9][0-9]*$/" > </td>                 </tr>                 <tr>                     <td><div align= "Ri Ght "> Address:</div>                     </td>     &N Bsp               <td><input type= "text" id= "txtaddress" ></td>   &N Bsp             </tr>                 <tr> & nbsp   &nbsp               <td><div align= "right" > Zip:</div>       & nbsp             </td>                   &NBSP ; <td><input type= "text" id= "Txtpostalcode" maxlength= "6" isvalidate= "true" errorsforvalidate= "ZIP code incorrect!" Validatepattern= "/^d{6}/" ></td>                 </tr>   &NBSP ;             <tr>                     <td><div align= "Right" > Operation:</div>                     </td>                     <td><input type= "button" value= "Register" id= "btnsubmit" onclick= "Btnsubmit_onclick ()" >  <input type= "reset" value= "reset" ></td>   & nbsp             </tr>             </table>         ;/form>     </body> </html>   in the above code, there is a commonfunction.js file, which is his content: The code is as follows:/******* * *     public JS function * ************************************* /function Commonfunction () {   /check value is null or empty     This.check IsEmpty    = function (obj)     {        VAR flag    = true;   &nbs P     for (var i=0 i<obj.length; i++)         {            V Ar e    = obj.item (i);             if (e.isrequired)             {    &NBSP ;               if (e.value== ')               &NBSP {                    alert (e.errorsforempty);       &NBS P             e.focus ();                     flag    = false;                     break;                            }               if (e.isvalidate)             {        &NBS P       if (This.checkvalidate (e) ==false)                 {  &NB Sp                 alert (e.errorsforvalidate);                     e.select ();                     e.focus ();       &NBSP             flag    = false;                     break;                            }        }           return flag;    }      //Check value is validate     this.checkvalidate    = function (E     {        var v    = e.value       if (v!= ')   & nbsp     {          return This.checkreg (E.validatepattern, v);         {   }      //RegExp validate     This.checkreg    = function (Patt Ern, value)     {        pattern    = pattern.substring (1, pattern.length-1); &nb Sp       VAR reg    = New RegEXP (pattern);         if (!reg.test (value))         {            R Eturn false;        }     {     //return a Element by ID object for what ID.     this. $getElementById    = function (ID)     {        var e   &N bsp;= document.getElementById (ID);           if (e!= ' undefined ')         {          &NB Sp return e;        }           return;    }      //Return a Element by name object for what ID.     this. $getElementsByName    = function (ID)     {        var e    = document.getelementsbyname (ID);           if (e!= ' undefined ')         {          &NBSp return e;        }           return;    }}            
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.