PDA

View Full Version : How do I first letter of each word upper case


dooballoh
02-25-2007, 11:48 PM
Hello.

I have learned about
$line =~ s/\b(\w)/\U$1/g;
to make the first letter of each word upper case.

However, how do I applied this to inputbox?
Example of inputbox:
<INPUT TYPE=\"text\" NAME=\"keywords\" SIZE=\"30\" MAXLENGTH=\"100\">
so that every time I type something in inputbox, it covert to first letter upper case and save to Mysql database.

It is for "Title Name" matters.

Please help!
Thank you in advance.

rwedge
02-26-2007, 02:43 AM
You could do this client side <script type="text/javascript">
<!-- /*<![CDATA[*/
function capitalizeMe(obj) {
val = obj.value;
newVal = '';
val = val.split(' ');
for(var c=0; c < val.length; c++) {
newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
}
obj.value = newVal;
}
/*]]>*/
// -->
</script>

<INPUT TYPE=\"text\" NAME=\"keywords\" SIZE=\"30\" MAXLENGTH=\"100\" onChange=\"capitalizeMe(this)\">


otherwise, apply the Perl regex to the value of keywords before writing to the database.

dooballoh
02-26-2007, 03:04 PM
Thank you "rwedge".

otherwise, apply the Perl regex to the value of keywords before writing to the database.

Yes, that's what I want to do instead of using javascript.
But, I don't know how. :D
How do I do that?
Help please.

Thank you in advance.

rwedge
02-26-2007, 11:23 PM
Could you supply the part of the perl code that handles the form input?

KevinADC
02-27-2007, 01:49 AM
ucfirst($string)

http://perldoc.perl.org/functions/ucfirst.html

dooballoh
02-27-2007, 09:24 AM
Could you supply the part of the perl code that handles the form input?

I tried make it simple as possible, codes are;

if ($FORM{'action'} eq 'add')
{
$error check ###error checking

if ($error_check eq 'ok')
{
$sql = INSERT INTO $sql_table_name VALUES ('$FORM{'keywords'}','$more_data')"; ###add to Mysql
&execute_sql;
print "
data has been added.
}
else
{
print "<P>Please go back and try again."; ###Mysql error msge
}
}
else
{
print "Error message, back and try again."; ###error msge
}
print "
<form name=\"formname\" action=\"script_name\" method=\"post\">
<input type=\"text\" name=\"keywords\" size=\"30\" maxlength=\"100\" />

#more inputbox
#more inputbox
#more inputbox

<input type=\"submit\" value=\"Add\" />
<input type=\"hidden\" name=\"action\" value=\"add\" />
</form>

I think that I know how to do output(print out data) using
$line =~ s/\b(\w)/\U$1/g;
or
ucfirst($string)
OR using CSS.
All I have been finding using these codes are for output functions.
I could not find any of them explaining how to use it in input functions.

But when it comes to "inputbox" (it would be typed in by users in blank empty box) and transfer that value whatever typed in "inputbox" to the Mysql and save it.

Let says;
1. If user type 'good man', I want it to make change that as 'Good Man' at the same time as they are typing.
The user actually can see it changing while they are typing.
2. Now, the empty inputbox filled in with 'Good Man' and they will submit as it is.
3. Then that value 'Good Man' will be saved in Mysql as 'Good Man'
4. The 'Good Man' will be appear when it calls by script to user's browser, even if they were typed 'good man' at the first place.

That is what I am trying to do.
Help please.

Thank you in advance

FishMonger
02-27-2007, 06:17 PM
1. If user type 'good man', I want it to make change that as 'Good Man' at the same time as they are typing.
The user actually can see it changing while they are typing.
That has nothing to do with Perl. The approach you're wanting is javascript, not Perl. You might want to ask your question in that forum.

BTW, you really should look into learning about the different quoting methods in Perl.

dooballoh
02-27-2007, 07:37 PM
Thank you, FishMonger. :)

I found out the way of doing I have explained above,
came out these;

1. Use CSS for input box. So that user can actually see that changes while they typing. Example;
In CSS part;
.cap {
text-transform: capitalize;
}
In input box;
<input type=\"text\" name=\"keywords\" size=\"30\" maxlength=\"100\" class=\"cap\" />
This will change from 'good man' to 'Good Man'
Problem is that this will saved in Mysql as 'good man' NOT 'Good Man'

2. So I added this code to database part.
$keywords =~ s/\b(\w)/\U$1/g;
This will change from 'good man' to 'Good Man' and saved as 'Good Man'

It works O.K. but I am not sure if this is the right way to doing thou.
Any better idea???:confused:

Help please.
Thank you in advance.

rwedge
02-27-2007, 11:49 PM
Looks like you have a handle on it. The css is just window dressing, javascript would actually save as caps, but the regex is the best way to insure that the text, if any, gets capitalized.

dooballoh
02-28-2007, 02:24 PM
Thank you rwedge, and FishMonger.

You guys are great. :thumbsup: