At least one space ...

S

shapper

Hello,

I need to check if a string contains at least one space between two
words:

"John" > Fail
"John " > Fail
"John Smith" > Pass

Basically I need to create a validation rule to be sure, as close as
possible, that a user inserted the first and last name.

The existence of at least two words and a space between them is the
closest approximation of it.

How can I do this?

Thanks,
Miguel
 
M

Martin Stoeckli

Hello Shapper

Call the "Trim" function and search for Spaces?

Best regards:
Martin
 
M

Michael Peyinghaus

shapper said:
Hello,

I need to check if a string contains at least one space between two
words:

"John" > Fail
"John " > Fail
"John Smith" > Pass

Basically I need to create a validation rule to be sure, as close as
possible, that a user inserted the first and last name.

The existence of at least two words and a space between them is the
closest approximation of it.

How can I do this?

Thanks,
Miguel

With RegEx, or a little bit more complicated:

string test = "asd";
if (test.Split(new string[] { " " },
StringSplitOptions.RemoveEmptyEntries).GetLength(0) > 1)
{
//do something
}
 
F

Family Tree Mike

shapper said:
Hello,

I need to check if a string contains at least one space between two
words:

"John" > Fail
"John " > Fail
"John Smith" > Pass

Basically I need to create a validation rule to be sure, as close as
possible, that a user inserted the first and last name.

The existence of at least two words and a space between them is the
closest approximation of it.

How can I do this?

Thanks,
Miguel

Use string.Split with the option of removing null entries. Check the
number returned to see that it is greater than 1, and then confirm each
entry in the result array is a "word", however you define that. I mean,
for example, is 123 a "word"? Does "John 3:16" pass?
 
A

Alberto Poblacion

Well, didn't think of that ... But yes it shouldn't pass.


Then you probably want to use RegEx:

string sample = "John 3:16";
Regex re = new Regex(@"^[A-Za-z]+(\s+\[A-Za-z]+)+$");
if (re.IsMatch(sample)) { ... }

The regular expression that I used can be read as:

^ = Start at the begining of the string
[A-Za-z]+ = At least one character from A to Z (upper or lowercase)
\s+ = At least one space
[A-Za-z]+ = At least one character from A to Z (upper or lowercase)
(...)+ = Repeat the block between parenthesis one or more times
$ = Anchor at the end of the string
 
S

Scott M.

shapper said:
Hello,

I need to check if a string contains at least one space between two
words:

"John" > Fail
"John " > Fail
"John Smith" > Pass

Basically I need to create a validation rule to be sure, as close as
possible, that a user inserted the first and last name.

The existence of at least two words and a space between them is the
closest approximation of it.

How can I do this?

Thanks,
Miguel

Taking a different approach and proceeding from your desire to ensure "that
a user inserted the first and last name", I would ask about where this code
will be in the larger application. If you simply need the end user to
supply a first and last name, the simplest approach is to give them two
textboxes to fill in and simply validate each box by doing a test for and
empty string.

Once the data is inputted, you can construct the FullName by taking the two
textbox values and joining them with a space in between. Now, you know for
a fact that the data is in the form you want and you can pass it over to the
code that does the next stage of processing, which may be your business
layer.

In short:

The UI layer collects and validates user input.
The Busienss layer processes the input.
The Data layer stores and retrieves input.

My point is that if you are in control of the user's input and need to
validate it, you can set up the UI to be the most intuitive to the end user,
but also the least error prone for your program. Ask yourself when the last
time you were asked to enter your full name into one text field, rather than
two of them.

-Scott
 
F

Family Tree Mike

Scott said:
Taking a different approach and proceeding from your desire to ensure "that
a user inserted the first and last name", I would ask about where this code
will be in the larger application. If you simply need the end user to
supply a first and last name, the simplest approach is to give them two
textboxes to fill in and simply validate each box by doing a test for and
empty string.

Once the data is inputted, you can construct the FullName by taking the two
textbox values and joining them with a space in between. Now, you know for
a fact that the data is in the form you want and you can pass it over to the
code that does the next stage of processing, which may be your business
layer.

In short:

The UI layer collects and validates user input.
The Busienss layer processes the input.
The Data layer stores and retrieves input.

My point is that if you are in control of the user's input and need to
validate it, you can set up the UI to be the most intuitive to the end user,
but also the least error prone for your program. Ask yourself when the last
time you were asked to enter your full name into one text field, rather than
two of them.

-Scott

That makes a lot of sense. Why should you build an app that tries to
figure out if "Lucy Van Pelt" is a first, middle, last name, or is Lucy
as the first name, and Van Pelt as the last name.
 
S

Scott M.

Family Tree Mike said:
That makes a lot of sense. Why should you build an app that tries to
figure out if "Lucy Van Pelt" is a first, middle, last name, or is Lucy as
the first name, and Van Pelt as the last name.

Correct. I just got done paying my bills online and every site wants me to
enter the pieces of my name in different textboxes. If my last name were
Van Pelt, then I'd enter that into the one last name textbox. The only
thing the site has to do is ensure that I did enter values in the required
boxes, which is a simple test for String.Empty.

-Scott
 
M

Mythran

Scott M. said:
Correct. I just got done paying my bills online and every site wants me
to enter the pieces of my name in different textboxes. If my last name
were Van Pelt, then I'd enter that into the one last name textbox. The
only thing the site has to do is ensure that I did enter values in the
required boxes, which is a simple test for String.Empty.

-Scott

Even then, does everyone in the world even have a last name? If not, is it
possible that someone in your target audience does not? These may need to
be taken in consideration as well.

Mythran
 
S

Scott M.

Mythran said:
Even then, does everyone in the world even have a last name? If not, is
it possible that someone in your target audience does not? These may need
to be taken in consideration as well.

Mythran

Perhaps. That certainly would be application-specific. When doing online
bill pay, you are pretty much required to provide a first and last name. I
think we've all been presented with input screens that were poorly designed
and require input in fields that someone may not be able to input data into.
My favorite is the security question "What is your mother's maiden name?".
Today, sites provide several choices for the question you want to use, but I
remember years ago one site that had this as the only security question.
Although I can answer that question, I wondered what someone who grew up in
an orphanage would answer to that? This is where users start entering dummy
data just to appease the form. Bad UI design!

-Scott
 
J

J.B. Moreno

Scott M. said:
My point is that if you are in control of the user's input and need to
validate it, you can set up the UI to be the most intuitive to the end user,
but also the least error prone for your program. Ask yourself when the last
time you were asked to enter your full name into one text field, rather than
two of them.

Well, I see it all of the time, when they ask for the name as it
appears on the card: but this doesn't invalidate your point, as there
may only BE one name on the card and so they are asking the appropriate
question.

Which is what I take your point to be: ask the question (for input)
that gets the precise answer that you need in your situation, not an
approximation that you then try to turn into the right answer.

(I'd like to make one more point, which is that you then STORE the
precise answer, and not a calculation based on it. Sooner or latter
you're going to want to go back to the right answer, and then you'll
have to try to reverse your calculation).
 
S

Scott M.

J.B. Moreno said:
Well, I see it all of the time, when they ask for the name as it
appears on the card: but this doesn't invalidate your point, as there
may only BE one name on the card and so they are asking the appropriate
question.

Which is what I take your point to be: ask the question (for input)
that gets the precise answer that you need in your situation, not an
approximation that you then try to turn into the right answer.

(I'd like to make one more point, which is that you then STORE the
precise answer, and not a calculation based on it. Sooner or latter
you're going to want to go back to the right answer, and then you'll
have to try to reverse your calculation).

Correct. As for the "enter your name as it appears on the card" example, I
run into that all the time, because my card shows my first initial and my
middle and last names (because) I don't use my first name, I use my middle
name. However, many sites (incorrectly) invalidate the first name field
when I enter a single character.

The problem here is that the issuer of the data (the CC company) is not the
consumer of the data (the merchant who takes the CC) and the merchant
doesn't have a full understanding of the data formats possible.

-Scott
 
H

Harlan Messinger

Family said:
That makes a lot of sense. Why should you build an app that tries to
figure out if "Lucy Van Pelt" is a first, middle, last name, or is Lucy
as the first name, and Van Pelt as the last name.

How often does an application need to do anything special with a
person's last name or first name independently of the rest of his name?
And look at all the people who have trouble entering their names when
given the first-name, last-name paradigm:

Felix Barton Smith (known as Bart or Barton, hates the John part)

Wang Zhongmao (person in China, family name first)

Kiraly Ferenc (person in Hungary, family name first)

Alberto Pablo Ortega Morales (person in Mexico, family name Ortega,
mother's family name Morales)

Alberto Paulo Soares Coelho (person in Portugal, family name Coelho,
mother's fmaily name Soares)

Suharto (former Indonesian president)
 
S

Scott M.

How often does an application need to do anything special with a person's
last name or first name independently of the rest of his name?

Um, all the time? If the name were entered as one piece of data, you
wouldn't be able to generate customer reports alphabatized by customer last
name. Being able to work with the first and last name independently is
usually very essential.
And look at all the people who have trouble entering their names when
given the first-name, last-name paradigm:

Felix Barton Smith (known as Bart or Barton, hates the John part)

I hate my first name. I don't go by it, nor do I respond if someone were to
call me it. But, it is my legal name and when I have to fill out anything
that wants my legal name, it's not a problem to do so. By the way, why
would the person you mention above (Bart) have a problem with the "John"
part, when according to what you wrote, John is not his name?
Wang Zhongmao (person in China, family name first)

Kiraly Ferenc (person in Hungary, family name first)

Alberto Pablo Ortega Morales (person in Mexico, family name Ortega,
mother's family name Morales)

Alberto Paulo Soares Coelho (person in Portugal, family name Coelho,
mother's fmaily name Soares)

If I were building a site that might need to accept data from these regions,
I'd build the UI accordingly.
Suharto (former Indonesian president)

Well, he was just a jerk anyway!

-Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top