I am trying to figure out a way to add in a script or string that would automatically capitalize everything if the user did not already do for their name, city, and e-mail address. The e-mail address isn't a huge deal but it would be nice to apply that rule to it to as some users like to capitalize everything in their e-mail address. I've done some research and found this in cflib, http://www.cflib.org/udf/nameCase but was not sure how I would apply this to my form. This form basically takes in all the information from the user who is registering, all their information is stored in their database where they can later access it again if they need to change anything. So basically a script that before it gets sent to the server everything is capitalized in the first letter and it would detect if everything is in CAPS and make it so only the first letter of the word is caps and everything is lower case. This would also apply to two words, where each word have a capital. Anyone have any idea how I can go about in doing this?
It sounds like you want a title case function. Can you give an example of the types of capitalization you would need? Ex: lower case words -> Lower case words lower case words -> Lower Case Words UPPER CASE WORDS -> Upper case words UPPER CASE WORDS -> Upper Case Words I might have a function for you, depending on what you need.
lower case words -> Lower Case Words UPPER CASE WORDS -> Upper Case Words UPPER CASE WITH LOWER CASe -> Upper Case Words Basically the universal format would be the 2nd one. "Upper Case Words" where the first letter thats passed to the database is converted into upper case.
Okay, here is a script I use... note that when you apply titlecase, you may run across exceptions. For instance, you may not want a state code converted to title case (ie, "Tx" vs "TX"). If so, just code the exceptions that you want before you return the string. +++++ function titleCase(string) { if (len(string) gt 1) { string = lcase(string); if (refind("^[a-z]", string)) { string = ucase(left(string, 1)) & right(string,(len(string) - 1 )); } next = refind("[[:space:][unct:]][a-z]", string); while (next) { if (next lt (len(string) - 1)) { string = left(string, (next)) & ucase(mid(string, next+1, 1)) & right(string, (len(string) - (next + 1))); } else { string = left(string, (next)) & ucase(right(string, 1)); } next = refind("[[:space:][unct:]][a-z]", string, next); } } else { string = ucase(string); } return string; }
I'm a little stumped. I have your code - [cfscript] function titleCase(string) { if (len(string) gt 1) { string = lcase(string); if (refind("^[a-z]", string)) { string = ucase(left(string, 1)) & right(string,(len(string) - 1 )); } next = refind("[[:space:][unct:]][a-z]", string); while (next) { if (next lt (len(string) - 1)) { string = left(string, (next)) & ucase(mid(string, next+1, 1)) & right(string, (len(string) - (next + 1))); } else { string = left(string, (next)) & ucase(right(string, 1)); } next = refind("[[:space:][unct:]][a-z]", string, next); } } else { string = ucase(string); } return string; } [/cfscript Code (markup): and here is my code for my forms: <h3>Contact Information</h3> <span class="style1" style="font-weight:normal">All fields with * are required</span><br /> <table width="600" border="0" cellpadding="5" cellspacing="0"> <form method="post" action="step1.cfm" name="SignUpForm"> <tr> <td class="style1" colspan="5"> <br /> <cfset fieldlist = "prefix,firstname,lastname,email,email2,address,city,state,zip,country,phone,username,password"> <cfloop list="#fieldlist#" index="field"> <cfset item = StructFind(errors, field)> <cfif trim(item) IS NOT ""> <cfloop list="#item#" delimiters="~" index="msg"> <cfoutput>#msg#<br /></cfoutput> </cfloop> </cfif> </cfloop> <br /> </td> </tr> <cfoutput> <input type="hidden" name="onUpdate" value="#onUpdate#" /> </cfoutput> <input type="hidden" name="WhichOne" value=""> <tr align="left" valign="top"> <td valign="top"> <b <cfif errors.prefix is not "">class="style1"</cfif> >Prefix:</b><span class="style1">*</span><br /> <select name="prefix" size="1"> <option <cfif trim(prefix) is "0">selected</cfif> value="0">Prefix</option> <option <cfif trim(prefix) is "Mr.">selected</cfif> value="Mr.">Mr. </option> <option <cfif trim(prefix) is "Ms.">selected</cfif> value="Ms.">Ms.</option> <option <cfif trim(prefix) is "Mrs.">selected</cfif> value="Mrs.">Mrs.</option> </select> </td> <td valign="top"> <b <cfif errors.firstname is not "">class="style1"</cfif> >First Name:</b><span class="style1">*</span><br /> <input type="text" size="25" maxlength="25" name="firstname" required="YES" message="Please enter your First Name" onChange="javascript:while(''+this.value.charAt(0)==' ')this.value=this.value.substring(1,this.value.length); while(''+this.value.charAt(this.value.length-1)==' ')this.value=this.value.substring(0,this.value.length-1); changeCase(this.form.firstname);" value="<cfoutput>#firstname#</cfoutput>" /> </td> <td valign="top"> <b>MI: </b><br /> <input type="text" size="3" maxlength="2" name="middlename" onChange="javascript:while(''+this.value.charAt(0)==' ')this.value=this.value.substring(1,this.value.length); while(''+this.value.charAt(this.value.length-1)==' ')this.value=this.value.substring(0,this.value.length-1); changeCase(this.form.middlename);" value="<cfoutput>#middlename#</cfoutput>" /> </td> <td valign="top"> <b <cfif errors.lastname is not "">class="style1"</cfif> >Last:</b><span class="style1">*</span><br /> <input type="text" size="25" maxlength="25" name="lastname" required="YES" message="Please enter you Last Name" onChange="javascript:while(''+this.value.charAt(0)==' ')this.value=this.value.substring(1,this.value.length); while(''+this.value.charAt(this.value.length-1)==' ')this.value=this.value.substring(0,this.value.length-1); changeCase(this.form.lastname);" value="<cfoutput>#lastname#</cfoutput>" /> </td> <td valign="top"> <b>Suffix: </b><br /> <select name="suffix" size="1"> <option <cfif suffix is "">selected</cfif> value="">Suffix</option> <option <cfif suffix is "Jr.">selected</cfif> value="Jr." >Jr.</option> <option <cfif suffix is "Sr.">selected</cfif> value="Sr.">Sr.</option> <option <cfif suffix is "II">selected</cfif> value="II">II</option> <option <cfif suffix is "III">selected</cfif> value="III">III</option> <option <cfif suffix is "IV">selected</cfif> value="IV">IV</option> <option <cfif suffix is "V">selected</cfif> value="V">V</option> <option <cfif suffix is "PhD">selected</cfif> value="PhD">PhD</option> <option <cfif suffix is "MD">selected</cfif> value="MD">MD</option> <option <cfif suffix is "CPA">selected</cfif> value="CPA">CPA</option> <option <cfif suffix is "Esq.">selected</cfif> value="Esq.">Esq.</option> </select> </td> </tr> <tr align="left" valign="top"> <td colspan="5" valign="top"> <b <cfif errors.email is not "">class="style1"</cfif> >Email:</b><span class="style1">*</span><br /> <input type="text" size="50" maxlength="100" name="email" required="YES" message="Please enter your Email Address" value="<cfoutput>#email#</cfoutput>" /> <br /> <b <cfif errors.email2 is not "">class="style1"</cfif> >Please confirm your Email:</b><span class="style1">*</span> <br /> <input type="text" size="50" maxlength="100" name="email2" value="<cfoutput>#email2#</cfoutput>" /> <!--- <br /> ---> <!--- <a href="url" target="_blank"><img src="url" width="185" height="60" border="0" /></a> ---> </td> </tr> </table> <table width="600" border="0" cellpadding="5" cellspacing="0"> <tr align="left" valign="top"> <td width="50%" valign="top"> <b <cfif errors.address is not "">class="style1"</cfif> >Street Address:</b><span class="style1">*</span><br /> <input type="text" size="42" maxlength="50" name="address" required="YES" message="Please enter your Home Address" value="<cfoutput>#address#</cfoutput>" /> </td> <td width="50%" valign="top"> <b>Address 2 :</b><br /> <input type="text" size="42" maxlength="50" name="address2" value="<cfoutput>#address2#</cfoutput>" /> </td> </tr> </table> <table width="600" border="0" cellpadding="5" cellspacing="0"> <tr align="left"> <td valign="top"> <b <cfif errors.city is not "">class="style1"</cfif> >City: </b><span class="style1"><font size="3">*</font></span><br /> <input type="text" size="15" maxlength="20" name="city" required="YES" message="Please enter your City" value="<cfoutput>#city#</cfoutput>" /> </td> <td valign="top"> <b <cfif errors.state is not "">class="style1"</cfif> >U.S. State:</b><span class="style1"><font size="3">*</font></span><br /> <select name="stateid" onChange="findUS();"> <option value="0">NOT APPLICABLE <cfoutput query="getstates"> <option <cfif stateid is sid>selected</cfif> value="#getstates.sid#">#getstates.statename# </cfoutput> </select> </td> <td valign="top"> <b <cfif errors.zip is not "">class="style1"</cfif> >Postal Code:</b><span class="style1"><font size="3">*</font></span><br /> <input type="text" size="10" maxlength="10" name="zip" required="YES" message="Please enter your Zip Code" value="<cfoutput>#zip#</cfoutput>" /> </td> </tr> <tr align="left"> <td valign="top"> <b <cfif errors.country is not "">class="style1"</cfif> >Country - Province:</b><span class="style1"><font size="3">*</font></span><br /> <select name="countryid" onChange="forceCountry();"> <option value=""> <cfoutput query="getcountries"> <option <cfif countryid is cid>selected</cfif> value="#getcountries.cid#">#getcountries.countryname# </cfoutput> </select> </td> <td valign="top"> <b <cfif errors.phone is not "">class="style1"</cfif> >Home Phone: </b><span class="style1"><font size="3">*</font></span><br /> <input onfocus="if (this.value == 'XXX-XXX-XXXX') this.value = '';" type="text" size="14" maxlength="20" name="phone" required="YES" message="Please enter your Home Phone Number" value="<cfoutput>#phone#</cfoutput>" /> </td> <td valign="top"> <b>Cell Phone: </b><br /> <input type="text" size="14" maxlength="20" name="cell" value="<cfoutput>#cell#</cfoutput>" /> </td> </tr> </table> <hr width="600" color="green"/> <table width="600" border="0" cellpadding="5" cellspacing="0"> <tr> <td colspan="6" align="left"><b>Choose a username and password:</b><span class="style1"><font size="3"><b>*</b></font></span>(<a style="text-decoration:none" onmouseover="TagToTip('Username', ABOVE, true, FADEIN, 500, FADEOUT, 1000, WIDTH, 250, BGCOLOR, '#FFFFCC')"><span class="style1">?</span></a>)<br /> <div id="Username"> Your Username and Password cannot be longer then 20 characters. <br /> Neither should include any characters that aren't letters or numbers.</div> </td> </tr> <tr align="left" valign="top"> <td nowrap="nowrap"><b <cfif errors.username is not "">class="style1"</cfif> >Username:</b><span class="style1"><font size="3"><b>*</b></font></span></td> <td><input type="text" size="10" maxlength="20" name="username" required="YES" message="Please enter a Username" value="<cfoutput>#username#</cfoutput>" /></td> <td nowrap="nowrap"><b <cfif errors.password is not "">class="style1"</cfif> >Password:<span class="style1"><font size="3">*</font></span></b></td> <td><input type="password" name="password" size="10" maxlength="20" required="YES" message="Please enter a password" value="<cfoutput>#password#</cfoutput>" /></td> <td nowrap="nowrap"><b <cfif errors.confirmpassword is not "">class="style1"</cfif> >Confirm Password:</b><span class="style1"><font size="3">*</font></span></td> <td><input type="password" name="confirmpassword" size="10" maxlength="20" required="YES" message="Please confirm your password" value="<cfoutput>#confirmpassword#</cfoutput>" /></td> </tr> </table> <hr width="600" color="green" /> <table width="600" border="0" cellpadding="5" cellspacing="0"> <tr align="left"> <td> <br /> <label><strong>Where did you hear about us?</strong> <select onChange="phanatic()" name="hearabout" size="1"> <option value="0">Click Arrow -----------> <cfoutput query="GetHearAbout"> <option <cfif hearabout is hearid>selected</cfif> value="#hearid#"> #hearaboutus# </cfoutput> </select> </label> <br /> <br /> </td> </tr> <tr> <td style="padding:0px"> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td width="67%">Note: Gaming Licensure requires a background check, which may be denied if you have prior felony convictions, or have previous had a gaming license denied or suspended</td> </tr> </table> </td> </tr> <tr> <td align="center"> <br /> <br /> <input <cfif agreecheck is not 0>checked</cfif> name="agreecheck" type="checkbox" onClick="agreesubmit(this)">I Agree to the <a href="url" target="234new">Terms of Service/Agreement<br></a> </td> </tr> <tr> <td> </td> </tr> <tr> <td> <br /> <div align="center"> <input name="btnSaveInfo" type="submit" value="Continue" <cfif agreecheck is 0> disabled="disabled"</cfif> /> </div> <br /> <cfif agreecheck is 0> <script type="text/javascript"> //change two names below to your form's names document.SignUpForm.agreecheck.checked=false </script> </cfif> </td> </tr> </form> Code (markup): Where would I be able to apply that so it gets parsed through the database with the right strings before it is saved
LOL... part of the original code got translated into a smiley. You'll have to change it back before you use the code. Anyway, clean the data after the form submission but before the DB write. You are submitting to step1.cfm, so you could probably clean the data on that page before you write it to the DB.
rob, i took parts of that code out, there is also a step1-save.cfm file as well that is associated with this...can you give me an example tho how i would wrap the titlecase with either the name field or city?
Post the code that you have for saving it to the DB, and I'll modify it so that it uses the titlecase function. Or, PM it to me and I'll take a look.
<cfif NOT StructIsEmpty(form)> <cfloop collection="#form#" item="i"> <cfset form[i] = titleCase(form[i]) /> </cfloop> </cfif> Code (markup): Now all the form values are titleCased.