ASP form validation

They have: 32 posts

Joined: Jun 2009

Need some help here!
I am a complete novice at ASP, and have downloaded some open source code, which I have got working, but would like to improve it a little.

If there are errors within the form, it redirects to a page, and says
"Errors detected, please go back and fix".

I would like it to list the fields where the errors have occured, ie:
"Errors detected, se below:
Telephone - please enter a number"

I have uploaded what I have so far:

Validation so far

Really hope somebody can help me....

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Here is the general rundown of how I do a form, I'll give the "basic" process of how I set the script up in PHP, and you should be able to follow over to ASP (ie. get rid of $ in front of variable names LOL)

- Create an array, say $aryErr. This will contain a list of error messages to display

- Check for post values, if so, then start the validation process.

--- For each item you check, instead of setting a main bool value of true or false like you are doing, we add an element to the array with the message. Again, you'll have to convert the idea over to ASP:

    if (strlen($_POST['Username']) < 3) $aryErr['UserName'] = "Username must be at least 3 characters";

--- At the end of all validation, while still inside the "if there are post values". we check how many elements $aryErr has, if it is none, then all validated, and now we process the form.

-- Now for the "ELSE" side of if there were post values (form submitted).

--- Set all "Post" variables to their default values. (usually, most are empty strings) You should do this here, as down in the form you will be displaying the these variables as the Value="" in the inputs

- Ok, at this point the post variables from the form should either contain what they submitted, or the default values for first time processing.

- Display the main code for the page up till where the "guts" of the form go.

-- IF there are post values, and count of aryError is zero, form was submitted, so show your "thank you" message

-- ELSE display the form for them to fill out since it is the first time or they entered something wrong

--- IF count of aryErrors is at least one, display a block letting them know they have errors, and loop through he array displaying each message

-- In each form input, do something like the following. Make sure you find an ASP equivilant to the htmlspecialchar() I use. Very important.
    <input type="text" name="UserName" value="<?php echo htmlspecialchar($_POST['UserName']); ?>" >

- close up the page with the rest fo the code.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.