0

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.

4
  • 2
    Check for a space in the string? But i would rather create 2 database columns and form fields (or better yet 3, firstname, middlename and lastname) instead of contactting them Commented Sep 20, 2024 at 11:05
  • 3
    But can't last names consist of more than one word? In Spanish for example it is very common. And Arab names often have 'Al' in them. Commented Sep 20, 2024 at 20:33
  • 5
    Falsehoods Programmers Believe About Names kalzumeus.com/2010/06/17/… Commented Sep 22, 2024 at 21:51
  • 1
    With names? No. You will NOT be able to enforce this. Many surnames really are two words, especially for those of hispanic origin, but other cultures as well. Likewise, many given names really are two words, especially for those from the American South (Bobby Sue, Sandra Mae, etc), but other cultures do this as well. Commented Sep 24, 2024 at 14:15

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

See here, vbscript allows the usage of regex.

(?=\S{1,50}\s\S{1,50}).{1,50} will check for 1 to 50 characters with one Whitespace in between.

You can apply the regex to your input with:

dim txtName
txtName = Request.Form("name")
Set re = New RegExp
re.Pattern    = "(?=\S{1,50}\s\S{1,50}).{1,50}"           
If NOT re.Test(txtName)  Then ProblemsWithRegister = 1

Detailed breakdown of the regex:

\S{1,50} 1 to 50 non Whitespace characters

\s 1 Whitespace

\S{1,50} 1 to 50 non Whitespace characters

(?=\S{1,50}\s\S{1,50})positive look ahead ensures that the match satisfies the \S{1,50}\s\S{1,50} condition

.{1,50} ensures match is between 1 and 50 characters long

However note that this satisfies your name check but won't work for all names of real people.

Comments

-3

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.

2 Comments

Why didn't you use a boolean for the parameter ProblemsWithRegister? That is just weird.
His original script used ProblemsWithRegister = 1 as the flag, so that is what I used. I don't think it deserves a thumbs down for trying to get this script to work with his existing script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.