jQuery validate form empty fields

validate empty fields using jQuery
In Web forms we need to validate fields on the form to check field should not be empty. For example for login form we need to validate textboxes for username and password to check textbox not empty.

Example to validate textbox not empty using jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Validate empty field using jQuery</title>

<script type="text/javascript">
$(document).ready(function () {


function Validate() {
var username = $("#username").val();
var password = $("#pwd").val();

if (username == "" && password == "") {
alert("Enter username and password");
return false;

if (username == "") {
alert("Enter username");
return false;

if (password == "") {
alert("Enter password");
return false;

<form id="form1" runat="server">
<label for="userName">Username</label>
<input type="text" name="log" id="username" value="" size="50" tabindex="10" />
<label for="pwd">Password</label> 
<input type="password" name="pwd" id="pwd" value="" size="50" tabindex="20" />
<input type="submit"  name="btnsubmit" id="btnsubmit" value="LogIn" onclick="return Validate();"/>


Post a Comment