Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch comma (,) in a string

Status
Not open for further replies.

jianhua

Programmer
Jun 22, 2001
55
US
Hi,

I have a form field with both the first name and the last name. The format is "last name, first name". When users update the field, I want to validate it and catch the error if comma is missing. Either javascript or cold fusion will be ok. I tried both, the problem was that comma is an operator and I couldn't escape it.

Cold fusion:
<cfif find(&quot;,&quot;, form.field) is 0>
error message here
</cfif>

Javascript:
var err = true;
var f=document.formname.fieldname.value;
for (var i=0; i < f.length; i++) {
var oneChar = f.toString().charAt(i);
if (oneChar == &quot;,&quot;){
err = false;
break;
}
}
if (err == true) {
alert(...);
}

Please help!!!
-J
 
All you have to do is replace the space with a comma you can be assured there will be a space between the names so try this
replaceSubstring(&quot;Last First&quot;, &quot; &quot;, &quot;,&quot;) = &quot;First,Last&quot;

If you want to keep a space like &quot;Last, First&quot;

replaceSubstring(&quot;Last First&quot;, &quot; &quot;, &quot;, &quot;) = &quot;Last, First&quot;

Hope this helps

Wes
 
What about if the last name is more than one word, for example: Smith Jr.?
 
Then use <cfset z = Replace(x, &quot;Guttman&quot;, &quot;Smith&quot;, &quot;One&quot;)>
<cfoutput>
#z#
</cfoutput>

This will replace the first space only and if the user has two first names good luck to you hehe.
 
You could also just add a text box for generation ie. JR
and append it to the value you insert. That would probably be the best.
 
The name data (last, first)is loaded from other resource. I just create a form to display it and let people update it online. Once it has been updated, I have to send back the data with the same format (last, first) nightly, so I tried to make things easy and just check whether there is a comma in that field when people update it. Any idea excluding adding more fields?
 
Problem solved. I use find(chr(44), form.field). Thanks, Wes!!
 
Code:
<CFIF string does not contain &quot;,&quot;>
 ...error...
<CFELSE>
 ...good to go...
</CFIF>

at least for future reference :) Did I help?
Vote!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top