Validating TextBox text using Java Script.

Posted on 2013-06-18
Last Modified: 2013-06-29
This is for web form developed with .Net 3.5 and VS2010.I want to validate the text entered in Text Boxes using Java Script on submission.

It shouldn't submit to the database if entered Text contains urls,html tags,scripts and anything dangerous.

How to validate the urls,html tags,scripts using Java Script
Question by:KavyaVS

Assisted Solution

MrC63 earned 100 total points
ID: 39257713
Here's a simple way:

Create an array of strings that are not allowed, then test the text box contents against the array list.  Then use this array to test against the actual text box contents.  See the attached sample javascript.

The "Submit button would be changed to a button type (<input type=button> instead of <input type=submit>.  You would then add an onClick event that calls the TestSubmission function:

<input type="button" onClick="TestSubmission() ;" value="Save">

The array can contain any string of characters you want to test for that are not allowed during the form submission.

Hope that helps you, let me know if you need further clarification.

LVL 12

Assisted Solution

by:jitendra patil
jitendra patil earned 350 total points
ID: 39258959
hi kavyavs

refer the below link,
Coding techniques for protecting against Sql injection
it will help you check in your whole application's textboxes in will be always helpful
you need to add your htmlinjection text  ie(urls,html tags,scripts) in the blacklist array.
it will be once for all using Modules.

Hope this helps.

Assisted Solution

PagodNaUtak earned 50 total points
ID: 39259086

Validating data inputted by users using javascript alone is a very bad idea. As users of sites can disable javascript to get pass your validation.

It would be better that there is a client and server side validation.
LVL 12

Accepted Solution

jitendra patil earned 350 total points
ID: 39259501
Try to implement the way i have described above as it will be coded for once and will be used in your overall application, you won't need to be calling function for each and every textbox, using module you will be doing it once and used for all.

Coding techniques for protecting against Sql injection RSS
Try the link given above, its a tested and proven approach.

Hope this helps.

Author Closing Comment

ID: 39287597

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can display objects ? 3 24
Why does my button disappear? 3 28
Turn on intranet settings 1 41
Gridview alignment 1 10
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now