本篇文章主要是對js中判斷用戶輸入的值是否為空的簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
在js中判斷用戶輸入的值是否為空,這是大家用得非常多的. 這沒有什麼好寫的. 而我卻寫了. 原因只是自以為是的認為我的這些代碼寫得不錯, 供大家參考一下. 這是摘自的我一個項目的中的用戶注冊頁面.對於大多數人來說,這都幾乎是100%經歷過的. 貼代碼吧,這些代碼都是用js寫的. 不難,很容易看懂. 看的時候,只要區別兩個js類就行了. 前台頁面代碼:reguser.aspx 代碼如下: <%@ 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> <title>用戶注冊</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <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> <script> 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 = ''; reg.address = ''; reg.postalcode = ''; reg.form = null; function btnSubmit_onclick() { reg.form = document.forms[0]; var comFun = new commonFunction(); if(!comFun.checkIsEmpty(reg.form)) { return false; } if(comFun.$getElementById('txtPassword').value!=comFun.$getElementById('txtConfirmPassword').value) { alert('兩次密碼輸入不一致'); comFun.$getElementById('txtConfirmPassword').select(); return false; } reg.userName = comFun.$getElementById('txtUserName'); reg.password = comFun.$getElementById('txtPassword'); reg.question = comFun.$getElementById('txtQuestion'); reg.answer = comFun.$getElementById('txtAnswer'); reg.url = comFun.$getElementById('txtUrl'); reg.email = comFun.$getElementById('txtEmail'); reg.tel = comFun.$getElementById('txtTel'); reg.mobile = comFun.$getElementById('txtMobile'); reg.qq = comFun.$getElementById('txtQQ'); reg.address = comFun.$getElementById('txtAddress'); reg.postalcode = comFun.$getElementById('txtPostalcode'); var es = comFun.$getElementsByName('sex'); var eL = es.length; for(var i=0; i<eL; i++) { var e = es[i]; if(e.checked) { reg.sex = e.value; break; } } 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); } function callback_Reg(res) { var rv = res.value; if(rv) { alert('注冊成功!'); window.location.href='/Default.aspx'; } else { alert('有錯誤發生,注冊失敗!有可能是用戶名或者域名被別人注冊過了!'); } } // 檢測用戶名的域名是否被其它用戶注冊過了 function checkIsRegistered(obj, errorS, t) { var v = obj.value;