Simple JavaScript textbox validation example in Asp.Net

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

New to Office 365 SharePoint Online? Get Office 365 Enterprise E3 Subscription & Try out all the features

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

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” “”>
<html xmlns=””>
<head runat=”server”>
<title>Simple JavaScript validation example</title>
<script language=”javascript” type=”text/javascript”>
function validateName() {
if (document.getElementById(“txtName”).value == “”) {
alert(‘Enter Name’);
return false;
<form id=”form1″ runat=”server”>
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();” />

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

Check out Best Alternative to InfoPath -> Try Now


(Installation & Features)

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

View all posts by Bijay Kumar →