Capitalize first letter of a field

A place for users and developers of the Xataface to discuss and receive support.

Capitalize first letter of a field

Postby deswong » Thu Jan 15, 2009 3:50 pm

Hi,

Is there any easy way to do this? I have a very simple database to start with, where I have two columns: ID, Colour.

I want to be able to ensure that on entry of Colour (ID is auto-incrementing) that the first letter is capitalized, just for consistency.

Regards,
Des.
deswong
 
Posts: 5
Joined: Thu Jan 15, 2009 2:39 pm
Location: Brisbane

Postby deswong » Thu Jan 15, 2009 4:30 pm

I've found code as follows:
<SCRIPT LANGUAGE="Javascript">
<!-- Begin
function capsLc(){
if (navigator.appVersion.substring(0,1)=="2"){
navOld();
}
else navNew();
}
function navOld(){
txt=document.isn.caps.value+" ";
txt=txt.toLowerCase();
txtl="";
while ((txt.length>0)&&(txt.indexOf(" ")>-1)){
pos=txt.indexOf(" ");
wrd=txt.substring(0,pos);
cmp=" "+wrd+" ";
if (tst.indexOf(cmp)<0){
ltr=wrd.substring(0,1);
ltr=ltr.toUpperCase();
wrd=ltr+wrd.substring(1,wrd.length);
}
txtl+=wrd+" ";
txt=txt.substring((pos+1),txt.length);
}
ltr=txtl.substring(0,1);
ltr=ltr.toUpperCase();
txtl=ltr+txtl.substring(1,txtl.length-1);
document.isn.caps.value=txtl;
}
function navNew(){
txt=document.isn.caps.value+" ";
txt=txt.toLowerCase();
txtl="";
punc=",.?!:;)'";
punc+='"';
while ((txt.length>0)&&(txt.indexOf(" ")>-1)){
pos=txt.indexOf(" ");
wrd=txt.substring(0,pos);
wrdpre="";
if (punc.indexOf(wrd.substring(0,1))>-1){
wrdpre=wrd.substring(0,1);
wrd=wrd.substring(1,wrd.length);
}
cmp=" "+wrd+" ";
for (var i=0;i<9;i++){
p=wrd.indexOf(punc.substring(i,i+1));
if (p==wrd.length-1){
cmp=" "+wrd.substring(0,wrd.length-1)+" ";
i=9;
}
}
if (cmp.indexOf(cmp)<0){
ltr=wrd.substring(0,1);
ltr=ltr.toUpperCase();
wrd=ltr+wrd.substring(1,wrd.length);
}
txtl+=wrdpre+wrd+" ";
txt=txt.substring((pos+1),txt.length);
}
ltr=txtl.substring(0,1);
ltr=ltr.toUpperCase();
txtl=ltr+txtl.substring(1,txtl.length-1);
document.isn.caps.value=txtl;
}
// End -->
</script>
...
<INPUT TYPE="button" NAME="html1" VALUE=" Submit" onClick="capsLc()">
...

but how do I inegrate this with Xataface? Or is this redundant as there is something already in place?
deswong
 
Posts: 5
Joined: Thu Jan 15, 2009 2:39 pm
Location: Brisbane

Postby shannah » Thu Jan 15, 2009 5:37 pm

If all you want to do is make sure that colour is capitalized, you can do this easily in the beforeInsert() trigger in the delegate class.

e.g.

Code: Select all

function beforeInsert(&$record){
    if ( trim($record->val('Colour')) ){
        $record->setValue('Colour', ucfirst($record->val('Colour')));
    }
}

shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby deswong » Thu Jan 15, 2009 7:53 pm

Therefore if I wanted to cap the first letter and make the rest lowercase I would use:

$record->setValue('Type', ucfirst($record->val((strtolower('Type'))));


I haven't done PHP before so I am still trying to wrap my head around this. I love how easy it is to write a web database application with Xataface! I have done more in 1 1/2 days of "tinkering" than I have back in the days of using Java and writing all that code!
deswong
 
Posts: 5
Joined: Thu Jan 15, 2009 2:39 pm
Location: Brisbane

Postby deswong » Thu Jan 15, 2009 8:21 pm

Nevermind - took a while to work out - but for those who are interested I achieved this by doing:

Code: Select all
$record->setValue('Type', ucfirst(strtolower($record->val('Type'))));
deswong
 
Posts: 5
Joined: Thu Jan 15, 2009 2:39 pm
Location: Brisbane


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 28 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved