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.
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)
//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
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);
From server side(Code behind we need can call the function like the below code
//To restrict on drop of any special charcater and on paste of any special character we can use the below code from server side
Hope you will understand the code. Let me know if you have any queries. Again meet with new code…
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 ..
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
Hey Thank you.. quite useful post