Javascript - Beautiful Validation, No Extras.
gist.github.com
gist.github.com
return if not username
error: "No Username Given.", field: 'name'
else if not password
error: "No Password Given.", field: 'pass'
else if username.length < 3
error: "Username is less than 3 Characters.", field: 'name'
else if password.length < 4
error: "Password is less than 4 Characters.", field: 'pass'
else if not (/^([a-z0-9-_]+)$/i).test(username)
error: "Username contains invalid characters.", field: 'name'
else
false
Hopefully a bit more readable. Link to see the compiled JS output: http://goo.gl/QhR9c o 'PARAM_START ParamList PARAM_END FuncGlyph Block', -> new Code $2, $5, $4
;)If you want to impress me, develop some system that lets you write validation rules once and have them work on both the client and the server.
Back in my previous existence, before a strange series of events transformed me into an A.I. programmer, I was a generalist web developer. Back in 2001 I was helping some guys make a voice chat site aimed at Brazil, and against my advice, they implemented validation in Javascript and not on the server.
We launched and it all seemed OK for a few weeks until I get a call at 7 pm because the site is all f'ed up.
Well, doing some investigation I find that there's a user with the empty string for his user name and that this has had a bigger impact on the system than one might think.
I nuked the guy with the SQL monitor and added server-side validation that night.
Since then I worked on hundreds of different sites and web applications and I've had to deal with the busted app made by somebody who was too lazy to do server side validation many many times.
On one hand you've got the person who browses without Javascript turned on. Perhaps you're one of the cool kids who makes apps that don't work at all without Javascript, so you don't need to worry about him.
You still need to worry about the people that want to mess with you. If you build a community site that's substantial at all (say 10,000+ users) you're going to get hit. Today it's easier than ever to reverse-engineer client-server communication with Firebug and then use curl or another tool to make phony requests.
Apps built in this style, where all validation is done on the client have a surface area that's 100% soft underbelly.
Client-side validation is purely a means of reducing multiple requests / round-trips IMHO and it doesn't even have to be as reliable as a good server-side solution, but purely cover the basics, and provide quick feedback to the user while at the same time reducing load on the server.
I pity the person who'll be maintain this.
if(!username || username.trim() = '')
return { error: "No Username Given.", field: 'name' };
if(!password || password.trim() = '')
return { error: "No Password Given.", field: 'password' };
and so on.Is that not clever enough??? Even a monkey would understand this.
I like the declarative feel of the expression, but since it actually changes state, I find it dangerous, since you generally expect expressions to be side-effect free.
Edit: I guess the second username += '' should have been password += ''
validate presence_of 'name', { error: "No Username Given" }
in CoffeeScript. (https://gist.github.com/2595013)
http://docs.jquery.com/Plugins/Validation/
But yeah, ternary operators are a great way to save line space.
If you use CoffeeScript however, you also get expression if. Very handy.
Surely a better solution is something more declarative which is then wrapped up in a library of some sort that does the heavy lifting.
An example of declarative validation is in ASP.Net MVC 3 and above where you can define validation on the server side models, enable unobstrusive javascript and you get both server and client side validation where the client side work is done by jquery and the server side by the model binding infrastructure that turns http request data into controller inputs.
For more complicated forms using a tool like jQuery Validation plugin makes more sense: https://github.com/jzaefferer/jquery-validation
That being said, your statement makes me think that you are overlooking an important aspect of client-side validation: instant feedback for the user. It's not necessarily just about saving bandwidth and server resources, but providing a clean and responsive user interface.