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 well written, for your reference. one time.
This is taken from the user registration page of one of my projects. For most people, this is almost 100% experienced.
Post the code, these codes are all written in js. It is not difficult and easy to understand. When reading, you only need to distinguish between the two js classes.
Front page code: reguser.aspx
Copy the code 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>
<title>User Registration</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('The two password inputs are inconsistent');
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('Registration successful!');
window.location.href='/Default.aspx';
}
else
{
alert('An error occurred and registration failed! It is possible that the username or domain name has been registered by someone else!');
}
}
// Check whether the domain name of the user name has been registered by other users
function checkIsRegistered(obj, errorS, t)
{
var v = obj.value;
var rv = RegUser.CheckIsRegistered(v, t).value;
if(rv)
{
alert(errorS);
obj.select();
return false;
}
}
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table align="center" cellpadding="3" cellspacing="0">
<tr>
<td><div align="right">Username:</div>
</td>
<td><input type="text" id="txtUserName" onblur="checkIsRegistered(this, 'This username has already been registered, please use another one!', 1)" maxlength="15" isRequired="true" isValidate="true" errorSForEmpty="Username cannot be empty!" errorSForValidate="Error matching! Starts with a letter, allows 3-16 bytes, alphanumeric underscores and permitted security symbols!" validatePattern="/^[a-zA-Z][a-zA-Z0-9_$% ]{2,15}$/"><font color="#ff0000">*</font></td>
</tr>
<tr>
<td><div align="right">Password:</div>
</td>
<td><input type="password" id="txtPassword" maxlength="15" isRequired="true" isValidate="true" errorSForEmpty="Password cannot be empty!" errorSForValidate="Matching error! Requires 3-16 characters section, allowing alphanumeric underscores and permitted security symbols!!" validatePattern="/^[a-zA-Z0-9_$%]{2,15}$/"><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 question:</div>
</td>
<td><input type="text" id="txtQuestion" maxlength="50" isRequired="true" isValidate="true" errorSForEmpty="The password prompt question is not filled in!" errorSForValidate="The length must be between 8-50 characters There must be no spaces between words!" validatePattern="//S{8,50}/"><font color="#ff0000">*</font></td>
</tr>
<tr>
<td><div align="right">Password question answer:</div>
</td>
<td><input type="text" id="txtAnswer" maxlength="50" isRequired="true" isValidate="true" errorSForEmpty="The answer to the password question is not filled in!" errorSForValidate="The length must be between 8-50 characters There must be no spaces between words!" validatePattern="//S{8,50}/"><font color="#ff0000">*</font></td>
</tr>
<tr>
<td><div align="right">SiteUrl:</div>
</td>
<td><input type="text" id="txtUrl" onblur="checkIsRegistered(this, 'This Url has been registered, please use another one!', 2)" maxlength="20" isRequired="true" isValidate ="true" errorSForEmpty="Site Url cannot be empty!" errorSForValidate="Site Url format is wrong!" validatePattern="/^[a-zA-Z0-9]{1,20}$/"><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" >Female</td>
</tr>
<tr>
<td><div align="right">Email:</div>
</td>
<td><input type="text" id="txtEmail" isValidate="true" errorSForValidate="Email format is incorrect!" validatePattern="/^.+/@(/[?)[a-zA-Z0- 9/-/.]+/.([a-zA-Z]{2,3}|[0-9]{1,3})(/]?)$/"></td>
</tr>
<tr>
<td><div align="right">Landline:</div>
</td>
<td><input type="text" id="txtTel" isRequired="true" isValidate="true" errorSForEmpty="The landline cannot be empty!" errorSForValidate="The landline format is wrong! Please use the format of 0592-5555555 !" validatePattern="/^(/d{3}-|/d{4}-)?(/d{8}|/d{7})$/"><font color="#ff0000">*</font></td>
</tr>
<tr>
<td><div align="right">Mobile phone:</div>
</td>
<td><input type="text" id="txtMobile" isValidate="true" errorSForValidate="Mobile phone format is incorrect!" validatePattern="/^1/d{10}$/"></td>
</tr>
<tr>
<td><div align="right">QQ:</div>
</td>
<td><input type="text" id="txtQQ" isValidate="true" errorSForValidate="QQ format is incorrect!" validatePattern="/^[1-9]*[1-9][0-9] *$/"></td>
</tr>
<tr>
<td><div align="right">Address:</div>
</td>
<td><input type="text" id="txtAddress"></td>
</tr>
<tr>
<td><div align="right">Zip code:</div>
</td>
<td><input type="text" id="txtPostalcode" maxlength="6" isValidate="true" errorSForValidate="The postal code is incorrect!" validatePattern="/^/d{6}/"></td>
</tr>
<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>
</tr>
</table>
</form>
</body>
</html>
In the above code, a CommonFunction.js file is included. Here is its content:
Copy the code code as follows:
/************************************************ **********
*
* Public js function
*
*************************************************** *********/
function commonFunction()
{
// check value is null or empty
this.checkIsEmpty = function(obj)
{
var flag = true;
for(var i=0; i<obj.length; 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 validated
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;
}
}
Post a picture of the effect: