Monday, June 18, 2012

Javascript code that can verify if special characters are used in a textbox

In the event that you would like to use client-side code to (1) inform a user that he/she has entered invalid characters in a textbox control, (2) highlight that control, and (3) place the focus on it, the following Javascript code can be added to a validation function to carry out all of three of those actions:

function ValidateRequest() { ... if (form.userinput_textbox.value != 0)
{ var userInputField = form.userinput_textbox.value; var invalidChars = "/!@#$%^&*(){}[]<>\\"; for (var i = 0; i < userInputField.length; i++) { if (invalidChars.indexOf(userInputField.charAt(i)) != -1) { window.scrollTo(0, 0); alert ("Please don't use any special characters or punctuation within the given textbox control."); form.userinput_textbox.backgroundColor = "C6ECFF"; form.userinput_textbox.focus(); return false; } } } ... }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.