Avoid special characters and space to enter in textbox using Jquery
As before we learned about how Validate strong password usingRegularExpressionValidator where we
choose characters,numbers,1 upper case letter and special character but suppose
someone wants that restrict to special character and space in password validation
so that time this code is very helpful .Mainly we need to avoid special characters
and space to validate password and username
.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
$('#txtusername').keypress(function
(key) {
var
regexpns = new RegExp("^[a-zA-Z0-9]+$");
var key
= String.fromCharCode(event.charCode ? event.which : event.charCode);
if (!regexpns.test(key))
{
event.preventDefault();
document.getElementById("lbresult").style.display
= 'block';
return false;
}
else
{
document.getElementById("lbresult").style.display
= 'none';
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:450px;">
<legend>Vaildate
textbox with special characters and space using Jquery</legend>
Username: <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
<label id="lbresult" style="color:
Red; display: none" >Space and Special characters not allowed</label>
</fieldset>
</div>
</form>
</body>
</html>
Out-Put:-
Avoid special characters and space to enter in textbox using Jquery
Reviewed by NEERAJ SRIVASTAVA
on
4:08:00 PM
Rating:
No comments: