Simple JavaScript textbox validation example in Asp.Net

InfoPath alternatives for form designing SharePoint

In this post we will discuss about a simple textbox required field validation in Asp.Net using
JavaScript.

Also you can check my previous articles on

How to open a hyperlink in a new tab using jQuery or JavaScript?

How to disable right click by using jQuery in asp.net?

Disable the multiline textbox and single line textbox in SharePoint using jQuery

Here in this exapmle when ever a user clicks on the submit button without entering anything in the
textbox, one alert message will be shown as “Enter Name”.

We have written the JavaScript function and calling that function in the OnClientClick property of the button.

Below is the full code:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”JavaScriptValidation.aspx.cs”
Inherits=”JavaScriptValidation” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Simple JavaScript validation example</title>
<script language=”javascript” type=”text/javascript”>
function validateName() {
if (document.getElementById(“txtName”).value == “”) {
document.getElementById(“txtName”).focus();
alert(‘Enter Name’);
return false;
}
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
Enter Name:<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>
<br />
<asp:Button ID=”btnTest” runat=”server” Text=”Click to Validate” OnClick=”btnTest_Click”
OnClientClick=”return validateName();” />
</div>
</form>
</body>
</html>


The output will be like as shown in the figure below:


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply