Ian
Ian

Reputation: 11

Request form checking length and two words

I have this code on my register page for a displayed name:

If (Len(Request.Form("name")) < 3) Or (Len(Request.Form("name")) > 50) Then                         ProblemsWithRegister = 1

Since I'd like users to use a name/surname format, is there a way to add a check that not only checks the number of characters (now < 3 > 50), but also checks if it's one word only? Thanks for those willing to help.

I tried to add two separate fields, but didn't like the concat to add them together in the db name field. Also because users seemed to confuse what had to be input.

Upvotes: -1

Views: 97

Answers (2)

easleyfixed
easleyfixed

Reputation: 502

You could try something like this if you must use a single name field. We get the value from the form, convert it into an array and count the array slots, if it has just one slot it is a single name, then we can do the other length checks to set the flag to 1 if its an error due to these length checks.

DIM FORMSTRING                                                                      'Declare the variable
IF Request.Form("name")     <> "" THEN                                              'If the form has a value
    NAMESTRING              = Request.Form("name")                                  'Set the variable to the value from the form
    NAMEARRAY               = Split(trim(NAMESTRING)," ")                           'Split the string into an Array on the spaces
    NAMEBOUND               = UBound(NAMEARRAY)                                     'Count how many slots in the array
    IF NAMEBOUND            = 0     THEN ProblemsWithRegister = 1                   'If only 1 slot, its a single name
    IF LEN(NAMESTRING)      < 3     THEN ProblemsWithRegister = 1                   'Detect if length is less than 3
    IF LEN(NAMESTRING)      > 50    THEN ProblemsWithRegister = 1                   'Detect if length is more than 50
    IF ProblemsWithRegister = 1 THEN Response.Write "Problems Detected!<br>"        'Report to screen if error detected
END IF                                                                              'End the if/the block

Note that use of an array might feel like over-kill, however later if you ever decide to add more columns to support more than just a single name column, you can just use each slot of the array to break out the values for each column.

Upvotes: -2

martigej
martigej

Reputation: 23

To verify if the response have more than one word, you can check if there is a space in the string. You can try:

dim txtName
txtName = Request.Form("name")
If (Len(txtName) < 3) Or (Len(txtName) > 50) Or InStr(txtName, " ") > 0  Then ProblemsWithRegister = 1

Upvotes: -1

Related Questions