To validate textbox in javascript

Dear all,

Validating through server side is required to check like the same name that the user entered is already there in database or not. Simple validations like user entered correct text or is the user had entered some special characters which will crash application. We can restrict entering special characters. On key press we can restrict entering the values by the user, but how can we restrict the user on paste of the special characters in a text box. Of course we can restrict paste of drag and drop on a text box.

Let us see how can do this using asp.net application with JavaScript



To restrict some special character we need to add escape sequence

Check the some of the special characters which requires escape sequence

//for slash it requires \\\\ etc.,

var r = new RegExp("[.,_0-9a-zA-Z'&\r \\\\]", "g");

//2 argument that we need to pass is Client id of the text box which you want to check and the maximum length of the text that you want to allow in a text box

function RestrictDescription(txtName, maxLength)
{
//This will capture the keycode when the user pressed any key in application
var exp = String.fromCharCode(window.event.keyCode)
var address=document.getElementById(txtName);
//Below line will have the special characters that is not allowed you can add if you want more for some characters you need to add escape sequence
var r = new RegExp("[-_,/'().0-9a-zA-Z \r]", "g");
if (exp.match(r) == null)
{
window.event.keyCode = 0
return false;
}
if(address.value.length >= maxLength)
{
alert("Maximum of 255 characters are allowed to be entered as a description");
//Truncated to 255 character and stored in the textbox
address.value = address.value.substring(0, maxLength);
return false;
}
}




From server side(Code behind we need can call the function like the below code



txtsubmit.Attributes.Add("onkeypress", "javascript:return RestrictDescription('" + txtsubmit.ClientID + "',255)");

//To restrict on drop of any special charcater and on paste of any special character we can use the below code from server side

txtsubmit.Attributes.Add("ondrop", "javascript:return false");

txtsubmit.Attributes.Add("onpaste", "javascript:return false");





Hope you will understand the code. Let me know if you have any queries. Again meet with new code…

Regards
JK


Comments

Author: Aluri Balahari30 Aug 2010 Member Level: Bronze   Points : 1

hi jk,i like that validation for special charectors but i need to check on client side not in the server side ,i mean inline code ,you just did in the client and server side as well i dont want that ,can you pls send me the client side with the input field htmal controls ..
thank you

Balu

Author: D.Jeya kumar(JK)30 Aug 2010 Member Level: Gold   Points : 2

Hi,

This will be executed in the client side only. We are just calling the client side event from the server side.

You can call the ondrop, onpaste, onkeypress event from java script.

Check the below code



ondrop="RestrictDescription(id,50)" onpaste="RestrictDescription(id,50)" onkeypress="RestrictDescription(id,50)"

Add the above code to your html inputbox


Guest Author: Saraswathi27 Jun 2012

Hey Thank you.. quite useful post



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: