Take the 2-minute tour ×

I'm looking to validate text in a text-area based on several regex patterns on a line by line basis. What's the best way to approach this? The way I've thought of is to use the the .change() event and split the text at line break characters. Then looping over each line and checking validity. This approach seems quite wasteful though, is there a more efficient way?

share|improve this question
I would have a look at Reaction Extensions for JavaScript, particularly the Throttle function, here is a blog post about it bit.ly/goSwJw I've never used this myself so I'm posting this as a comment instead of an answer. Good Luck! –  eSniff Dec 29 '10 at 1:15
What are you checking for....invalid chars? No reason to split into lines. –  ken Jan 2 '11 at 6:01
I need to split it into lines because as posted in the question it's a text area and each line represents an entirely separate input, and it is not related to the rest. –  Radu Jan 2 '11 at 22:05
You actually don't mention anything at all in the question about why it must be split into lines; you only say that you do it. Anyways, my question remains: what are you checking for? It's impossible to proceed without knowing that. Also, why are you thinking that your current method is "wasteful"? Do previous lines never change or something? And why are you abusing the textarea? Why not use DOM structures that are appropriate; either a separate text input for each 'input', or a separate textarea for each 'input'? I will tell you now, abusing HTML elements will cost you later. –  ken Jan 3 '11 at 18:54
First part is irrelevant to the question. I'm not abusing the textarea. If you have nothing constructive to add please don't add it. –  Radu Jan 3 '11 at 19:15
show 2 more comments

1 Answer

up vote 1 down vote accepted

Probably you're not using ASP.Net, but if you would, you could use data annotations, create a that rule and place it above the textarea corresponding property. That same exact and centralized rule will work for client and server side.

If, by any chance, you can use data annotations, do it. Otherwise, you can use the method you described using the change event. You could also use the blur method or, even if you use change, only execute the validation test if the last key inserted was enter. This way, you could at least save some client resources.

share|improve this answer
I am using asp.net and already doing server-side validation but I'd like to present the user with immediate feedback. –  Radu Jan 1 '11 at 18:24
Then, if you have client-side validation active, the input is tested for the first time when your field looses focus. It seems to me that you want to keep testing while the user is writing. If I understood right, I guess you could trigger the change event of textarea. Search the web for something like $("#textarea").change() or something in order to do the validation test everytime the text changes. –  Bomboca Jan 1 '11 at 18:30
I think the above would work perfectly. Anyway, there is another option. You may create a new action: That accepts a model with only the textarea property that has that validation rule. You may ask the server to validate the current text if the user hasn't typed anything in the last second or two. If you would have trouble with the first, you could surely do this, although I believe the first is best. Choose the one that fits you best. –  Bomboca Jan 1 '11 at 18:36
add comment

Your Answer


