views:

21

answers:

1

I need to validate a given Name field in the asp.net form.

User Name: Alex Brandon

In the database, we have a users table with user_name column.

I need to validate on lost focus of User Name form field, that it exists in the users table and is valid else show an alert.

Does somebody has a ready solution to this?

+1  A: 

Do you want to do it in javascript, or are you planning on a trip to the server? If it's a trip to the server (via asp.net ajax or a postback) you'd just have to.

select * from users where user_name=@name

and set the name parameter to your textbox value...

Note that unless you care about case, you may want to uppercase the two names for comparison.

Kendrick
1. Don't `SELECT *` ever, but especially for a simple existence test. Do you really want to bring all those columns back? 2. Why would you use `LIKE` instead of `=`? Are you really expecting the user to enter wild cards?
Joe Stefanelli
+1 for sanity, too bad you don't get rep from your comment. I'd either use like or convert to a case (and it would be better to convert to uppper/lower case since you don't want wild cards). Obviously time for me to go home after I fix my answer.
Kendrick
There should be no server round-trip. With Ajax, are you referring to do it using xmlhttp request? If so, can you pls explain more?
prabhats.net
Without going to the server, you're going to need to populate a list of names into the page itself, which people accessing the page will be able to see in the source. If you're using .NET, a round-trip is probably your best bet. Take a look at http://www.asp.net/ajaxlibrary/act.ashx ... Once you get it up and running, it's like magic. Does all the heavy lifting and doesn't get in the way of your code at all (until you do something unexpected, but you should be safe here...)
Kendrick
I agree. However, the architecture of the current application is not so robust. I am in production support and this name field is an auto suggest box implemented using javascript. All I need to do is to enahance this javascript to include validations.
prabhats.net
If you've already got an auto-suggets box, can you get access to that API and find if the entered name is in the list?
Kendrick
Yes this helped me. Thanks Kendrick.
prabhats.net