javascript to validate textbox

Posted by Rickeybglr under ASP.NET on 9/6/2012 | Points: 10 | Views : 4634 | Status : [Member] | Replies : 7
i want to write javascript which shud allow only alphanumric values, no special characters.
problem is tht i have 100 of textboxes in my page and want to apply ths javascript on all the textboxes of aspx page




Responses

Posted by: Wadhwanisanju on: 9/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hello,

There are two ways to do this task.

1 Through Regular Expression.
2 Write own code of JavaScript.

Here i am attaching a code snippet of JavaScript function which will automatically find the special character of given control id as input to it.

<script language="javascript" >
function CheckSpecialChar(id){

val=document.getElementById('id').value;

var splcharacter = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

for (var i = 0; i < val.length; i++) {
if (splcharacter.indexOf(val.charAt(i)) != -1) {
alert ("Special characters are not allowed.\n");
return false;
}
}
}
</script>

To call this function

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" OnClientClick="CheckSpecialChar('TextBox1')" Text="Button" />

This is how it will validate the things.

Happy coding!!!!

Thanks


Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 9/6/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this,

<script type="text/javascript">
function isAlphaNumeric(e) {
var key;
if (window.event) {
key = window.event.keyCode;
}
else {
key = e.which;
}
if (!((key > 64 && key <= 90) || (key > 96 && key <= 122) || (key > 47 && key <= 57) || (key == 8) || (key == 0) || (key == 127))) {
alert("You can enter only characters a to z,A to Z,0 to 9");
return false;
}
}

</script>

 

<asp:TextBox ID="TextBox1" runat="server" OnKeypress="javascript:return isAlphaNumeric(event,this.value);"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" OnKeypress="javascript:return isAlphaNumeric(event,this.value);"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" OnKeypress="javascript:return isAlphaNumeric(event,this.value);"></asp:TextBox>


Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 9/6/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
^ [a-z0-9]+$/i   

This is the regular Expression which validates the alphanumeric , write a function on the java script and use OnClientClick (as mentioned above)


Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijay.K on: 9/6/2012 [Member] Starter | Points: 25

Up
0
Down
var test = document.getElementById("txt1");
if(!/^[a-z0-9-]+$/i.test(test.value)) {
alert('Name can only be alpha numeric with hypen.');
return;
}



vijay.k

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/7/2012 [Member] Bronze | Points: 25

Up
0
Down
function validate(){
var usrid=document.getElementById("txtUserid"); //txtUserid-->ID of textbox
var alphanum=/^[0-9a-bA-B]+$/; //This contains A to Z , 0 to 9 and A to B
if(usrid.value.match(alphanum)){
return true;
}else{
alert(Put a Valid Userid Name);
return false;
}
}

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sgtammana on: 9/7/2012 [Member] Starter | Points: 25

Up
0
Down
hi,
By using Jquery we can bind the validation function to all the textboxes. for this we have to add the jquey js file into your applicaiton.

In the html header Page tag you have to put this code and in that you have to call the funtion.
<script type="text/javascript">
$(document).ready(function () {
SettingValidationMthodToTextbox();
});
</Script>
binding the function for the text.
function SettingValidationMthodToTextbox() {
$('input:text').bind("OnKeypress", function (e) {
var key;
if (window.event) { key = window.event.keyCode; }
else { key = e.which; }
if (!((key > 64 && key <= 90) || (key > 96 && key <= 122) || (key > 47 && key <= 57) || (key == 8) || (key == 0) || (key == 127)))
{ alert("You can enter only characters a to z,A to Z,0 to 9"); return false; }
}
});
}



Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Ricky,
Please Mark as Answer if it helpful to you..That helps others who search the same...

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response