How to determine if it has a full name?

I have a field in which the user can enter the first and last name to fill out my form. Sometimes users put their name and generate empty fields in my database. Remember that I cannot completely change this method, because this form is part of a larger project and is used by other sites of my company.

This is the part of the code where I need validation. I already have a check that ensures that the file is not empty, but I need more to make sure that there are two elements in the field, separated by a space.

<input name="fullname" class="fullname"   type="text" value="#fullname#" maxlength="150"/>
            <cfif fullname eq '' and check2 eq 'check2'>
            <br /><span style="color:red">*you must enter your full name</span></cfif>

Check2 eq 'check2' checks if the form has already been submitted to ensure that the user submits their data twice.

I thought of using regular expressions for this, but unfortunately I am not very good at how to use regx in CF9 and the documentation through me through me.

I also thought of using Find or FindOneOF, any thoughts on this?

Also, I try to avoid using JQ, JS, etc., so please try to keep your suggestions based on the IF CF code.

Any help or various suggestions on how to solve this problem would be greatly appreciated.

+3
source share
2 answers

You can do something like this for server side validation:

<cfscript>
TheString = "ronger ddd";
TheString = trim(TheString); // get rid of beginning and ending spaces
SpaceAt = reFind(" ", TheString); // find the index of a space

// no space found -- one word
if (SpaceAt == 0) {
    FullNameHasSpace = false;
// at least one space was found -- more than one word
} else {
    FullNameHasSpace = true;
}
</cfscript>

<cfoutput>
<input type="input" value="#TheString#">
<cfif FullNameHasSpace eq true>
    <p>found space at position #SpaceAt#</p>    
    <p>Your data is good.</p>
<cfelse>
    <p>Did not find a space.</p>
    <p>Your data is bad.</p>
</cfif>
</cfoutput>
+1
source

This does not require a regular expression. A slightly simpler solution:

<cfset form.fullname = "Dave " />
<cfif listLen(form.fullname," ") GT 1> <!--- space-delimited list, no need for trimming or anything --->
   <!--- name has more than one 'piece' -- is good --->
<cfelse>
   <!--- name has only one 'piece' -- bad --->
</cfif>
+4
source

All Articles