Click to See Complete Forum and Search --> : formatting text submitted to a database


Ashcrapper
06-03-2004, 06:16 AM
Hi

Im trying to format text that is submitted from a form on my site and then inserted into an access database using the 'insert record' behaviour in dreamweaver mx 2004.

From what I have gathered dwmx puts the submission into an array and submits it in 1 go.

So Im assuming that using javascript when the form is submitted will be the way to go.

Basically, I need some fields from my form to be submitted in lowercase, some in UPPERCASE, and some in TitleCase.

Can anyone help me out here, Ive searched high and low for a solution but can find something that fits the bill.

An example of what I would need to do is:

(TC = title case)
(UC = upper case)
(SC = sentence case)
(LC = lower case)

name (TC)
email (LC)
tel
job title (TC)
grade (SC)
place of work (TC)
speciality (SC)
property name (TC)
street (TC)
locality (TC)
town (TC)
postcode (UC)
county (TC)


Thanks in advance for any help, its really appreciated.

Cheers
Ash

Pittimann
06-03-2004, 07:25 AM
Hi!

Sorry, English is a foreign language for me. Could you please point out, what the "??" mean?(TC = title case) ??
(UC = upper case)
(SC = sentence case) ??
(LC = lower case)Cheers - Pit

Ashcrapper
06-03-2004, 07:26 AM
Hi

This Would Be Title Case
This would be sentence case

:)

Cheers
Ash

Pittimann
06-03-2004, 08:43 AM
Hi!

Something like this:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<script language="JavaScript" type="text/javascript">
<!--
function setcases(frm){
frm.email.value=frm.email.value.toLowerCase();
frm.postcode.value=frm.postcode.value.toUpperCase();
frm.name.value=tC(frm.name.value);
frm.jobTitle.value=tC(frm.jobTitle.value);
frm.placeOfWork.value=tC(frm.placeOfWork.value);
frm.propertyName.value=tC(frm.propertyName.value);
frm.street.value=tC(frm.street.value);
frm.locality.value=tC(frm.locality.value);
frm.town.value=tC(frm.town.value);
frm.county.value=tC(frm.county.value);
frm.grade.value=sC(frm.grade.value);
frm.speciality.value=sC(frm.speciality.value);
}
function tC(val){
var temp=val.split(' ');
for (var i=0;i<temp.length;i++){
var temp2=temp[i].substring(0,1);
var temp3=temp[i].substring(1,temp[i].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
if(temp[i].indexOf("'")!=-1){
temp[i]=special(temp[i]);
}
}
temp=temp.join(' ');
return temp;
}
function sC(val){
var temp=val.split(' ');
var temp2=temp[0].substring(0,1);
var temp3=temp[0].substring(1,temp[0].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[0]=temp2+temp3;
for (var i=1;i<temp.length;i++){
temp2=temp[i].substring(0,1);
temp3=temp[i].substring(1,temp[i].length);
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
}
temp=temp.join(' ');
return temp;
}
function special(val){
var temp=val.split("'");
var temp2=temp[1].substring(0,1);
var temp3=temp[1].substring(1,temp[1].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp=temp[0]+"'"+temp2+temp3;
return temp;
}
//-->
</script>
<body>
<form>
<input type="text" name="name" value="my name o'hara"> Name<br>
<input type="text" name="email" value="IWillnottELl@yOu"> Email<br>
<input type="text" name="tel" value="01234567"> Phone<br>
<input type="text" name="jobTitle" value="mY jOb TItLe"> Job Title<br>
<input type="text" name="grade" value="this is my grAde"> Grade<br>
<input type="text" name="placeOfWork" value="sOmeWhere iN aN oFFIce"> Place Of Work<br>
<input type="text" name="speciality" value="this Is my speciality"> Speciality<br>
<input type="text" name="propertyName" value="proPerty NamE"> Property Name<br>
<input type="text" name="street" value="trafAlgar square"> Street<br>
<input type="text" name="locality" value="mY loCalIty"> Locality<br>
<input type="text" name="town" value="straTFord ON aVon"> Town<br>
<input type="text" name="postcode" value="w3H xZ4"> Postcode<br>
<input type="text" name="county" value="whatEver countY"> County<br>
<input type="button" value="set cases" onclick="setcases(this.form);">
</form>
</body>
</html>Please note: the sentence case stuff checks for the first letter of the first word (will be made upper case if it is not) and for the second to last letter in any word (will be made lower case if they are not); the first letters of the second to last word will NOT be treated (example: this Is my speciality; the "I" in "Is" remains upper case).

Regarding the the title case, I put an example (o'hara), which is treated in an own function; maybe other "special cases" come to your mind to be treated in a special way. The ' will have the effect that the letter before and the one after will become upper case.

You can integrate the code into your form validation. I guess, you already check for empty fields and (example) numerals only for the phone number, postcode format etc.; so at a point where your validation is complete, you can update the fields before submitting.

Cheers - Pit

Ashcrapper
06-03-2004, 09:12 AM
thats brilliant mate, thanks for taking the time to do that. really appreciate it!

ill give it a go now and see if I can manage to fail miserably! hahahah!

Pittimann
06-03-2004, 09:14 AM
Hi!

You're welcome! :D

If you face problems to integrate the stuff, just post again (maybe linking to a page with the entire code or putting your code here).

Cheers - Pit

Ashcrapper
06-03-2004, 09:16 AM
the bit where you have got

frm.

I take it that that is the form name?

Pittimann
06-03-2004, 09:22 AM
Hi!

In the button I put: onclick="setcases(this.form);
The function is: function setcases(frm)
So inside the function, I can use frm as an "abbreviation"; using frm means not to have to type document.forms[0].blahblah or document.formname.blahblah all the time.

frm.blahblah is just less code...

I didn't give the form a name at all, but due to the fact that it is being referred to by using the argument passed to the function, this is not necessary.

Cheers - Pit

Ashcrapper
06-03-2004, 09:44 AM
hehe, didnt work...

dont think im fully understanding whats going on. I tried it on a different form so changed a couple of the initial values.

I'll post the code if you dont mind, see if im doing something utterly stupid :)

Pittimann
06-03-2004, 09:46 AM
Hi!

Go ahead! I'll be out for a short time, but after having returned back, I will check, if someone dealt with it...

Cheers - Pit

Ashcrapper
06-03-2004, 10:05 AM
Too big for one post so ill post the script and form separately


<script language="JavaScript" type="text/javascript">
<!--
function setcases(frm){
frm.name.value=tC(frm.name.value);
frm.address1.value=tC(frm.address1.value);
frm.address2.value=tC(frm.address2.value);
frm.address3.value=tC(frm.address3.value);
frm.postcode.value=frm.postcode.value.toUpperCase();
frm.county.value=tC(frm.county.value);
frm.email.value=frm.email.value.toLowerCase();
frm.jobTitle.value=tC(frm.jobTitle.value);
frm.areaOfWork.value=tC(frm.areaOfWork.value);
frm.course.value=sC(frm.course.value);
frm.placeOfStudy.value=tC(frm.placeOfStudy.value);
frm.natureOfEnquiry.value=sC(frm.natureOfEnquiry.value);
}
function tC(val){
var temp=val.split(' ');
for (var i=0;i<temp.length;i++){
var temp2=temp[i].substring(0,1);
var temp3=temp[i].substring(1,temp[i].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
if(temp[i].indexOf("'")!=-1){
temp[i]=special(temp[i]);
}
}
temp=temp.join(' ');
return temp;
}
function sC(val){
var temp=val.split(' ');
var temp2=temp[0].substring(0,1);
var temp3=temp[0].substring(1,temp[0].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[0]=temp2+temp3;
for (var i=1;i<temp.length;i++){
temp2=temp[i].substring(0,1);
temp3=temp[i].substring(1,temp[i].length);
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
}
temp=temp.join(' ');
return temp;
}
function special(val){
var temp=val.split("'");
var temp2=temp[1].substring(0,1);
var temp3=temp[1].substring(1,temp[1].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp=temp[0]+"'"+temp2+temp3;
return temp;
}
//-->
</script>

Ashcrapper
06-03-2004, 10:06 AM
<form action="<%=MM_editAction%>" method="post" name="contactForm" id="contactForm">
<input name="name" type="text" class="inputs" id="name" size="30" />
<input name="address1" type="text" class="inputs" id="address1" size="30" />
<input name="address2" type="text" class="inputs" id="address2" size="30" />
<input name="address3" type="text" class="inputs" id="address3" size="30" />
<input name="postcode" type="text" class="inputs" id="postcode" size="30" />
<select name = "county" size=1>
<option value="none">Select county</option>
<option value="none">-----------------------------</option>
<option value="Anglesey">Anglesey, Wales</option>
<option value="Avon">Avon, England</option>
<option value="Bedfordshire">Bedfordshire, England</option>
<option value="Berkshire">Berkshire, England</option>
<option value="BordersRegion">Borders Region, Scotland</option>
<option value="Buckinghamshire">Buckinghamshire, England</option>
<option value="Cambridgeshire">Cambridgeshire, England</option>
<option value="Cheshire">Cheshire, England</option>
<option value="Cleveland">Cleveland, England</option>
<option value="Clwyd">Clwyd, Wales</option>
<option value="CentralRegion">Central Region, Scotland</option>
<option value="Cornwall">Cornwall, England</option>
<option value="Devon">Devon, England</option>
<option value="Dorset">Dorset, England</option>
<option value="Cumbria">Cumbria, England</option>
<option value="Derbyshire">Derbyshire, England</option>
<option value="DumfriesAndGalloway">Dumfries &amp; Galloway, Scotland</option>
<option value="Durham">Durham, England</option>
<option value="Dyfed">Dyfed, Wales</option>
<option value="Essex">Essex, England</option>
<option value="FairIsle">Fair Isle, Scotland</option>
<option value="FifeRegion">Fife Region, Scotland</option>
<option value="Gloucestershire">Gloucestershire, England</option>
<option value="Glamorgan">Glamorgan, Wales</option>
<option value="GrampianRegion">Grampian Region, Scotland</option>
<option value="Gwent">Gwent, Wales</option>
<option value="Gwynedd">Gwynedd, Wales</option>
<option value="Hampshire">Hampshire, England</option>
<option value="HerefordAndWorcester">Hereford &amp; Worcester, England</option>
<option value="Hertfordshire">Hertfordshire, England</option>
<option value="HighlandRegion">Highland Region, Scotland</option>
<option value="Humberside">Humberside, England</option>
<option value="IsleOfMan">Isle of Man, England</option>
<option value="IsleOfWight">Isle of Wight, England</option>
<option value="Kent">Kent, England</option>
<option value="Lancashire">Lancashire, England</option>
<option value="Leicestershire">Leicestershire,England</option>
<option value="Lincolnshire">Lincolnshire, England</option>
<option value="GreaterLondon">Greater London, England</option>
<option value="LothianRegion">Lothian Region, Scotland</option>
<option value="GreaterManchester">Greater Manchester, England</option>
<option value="Merseyside">Merseyside, England</option>
<option value="Norfolk">Norfolk, England</option>
<option value="Northamptonshire">Northamptonshire, England</option>
<option value="Northumberland">Northumberland, England</option>
<option value="NorthYorkshire">North Yorkshire, England</option>
<option value="Nottinghamshire">Nottinghamshire, England</option>
<option value="Orkney">Orkney, Scotland</option>
<option value="Oxfordshire">Oxfordshire, England</option>
<option value="Powys">Powys, Wales</option>
<option value="Shropshire">Shropshire, England</option>
<option value="ScillyIsles">Scilly Isles, England</option>
<option value="Shetland">Shetland, Scotland</option>
<option value="SouthYorkshire">South Yorkshire, England</option>
<option value="Staffordshire">Staffordshire, England</option>
<option value="Strathclyde">Strathclyde, Scotland</option>
<option value="Somerset">Somerset, England</option>
<option value="Suffolk">Suffolk, England</option>
<option value="Surrey">Surrey, England</option>
<option value="Sussex">Sussex, England</option>
<option value="Tayside">Tayside, Scotland</option>
<option value="TyneAndWear">Tyne &amp; Wear, England</option>
<option value="Warwickshire">Warwickshire, England</option>
<option value="WesternIsles">Western Isles, Scotland</option>
<option value="WestMidlands">West Midlands, England</option>
<option value="WestYorkshire">West Yorkshire, England</option>
<option value="Wiltshire">Wiltshire, England</option>
<option value="Antrim">Antrim, Northern Ireland</option>
<option value="Armagh">Armagh, Northern Ireland</option>
<option value="Down">Down, Northern Ireland</option>
<option value="Fermanagh">Fermanagh, Northern Ireland</option>
<option value="Londonderry">Londonderry, Northern Ireland</option>
<option value="Tyrone">Tyrone, Northern Ireland</option>
<option value="CIAL">Alderney, C.I.</option>
<option value="Alderney">Guernsey, C.I.</option>
<option value="Herm">Herm, C.I.</option>
<option value="Jersey">Jersey, C.I.</option>
<option value="Sark">Sark, C.I.</option>
</select>
<input name="tel" type="text" class="inputs" id="tel" size="30" />
<input name="email" type="text" class="inputs" id="email" size="30" />
<input name="jobTitle" type="text" class="inputs" id="jobTitle" size="30" />
<input name="areaOfWork" type="text" class="inputs" id="areaOfWork" size="30" />
<input name="course" type="text" class="inputs" id="course" size="30" />
<input name="placeOfStudy" type="text" class="inputs" id="placeOfStudy" size="30" />
<textarea name="natureOfEnquiry" cols="49" rows="6" class="inputs" id="natureOfEnquiry"></textarea>
<a href="javascript:document.contactForm.submit();" value="set cases" onclick="setcases(this.form);" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image17','','Assets/Images/Buttons/sendEnquiry_on.gif',1)"><img src="Assets/Images/Buttons/sendEnquiry_off.gif" alt="send my enquiry" name="Image17" width="96" height="19" border="0" id="Image17" /></a>
<input type="hidden" name="MM_insert" value="contactForm">
</form>



Its probably something really really stupid....

Pittimann
06-03-2004, 10:19 AM
Hi!

It is really something to be solved easily. You are using an anchor tag to call the function. This is NOT a form element, so 'this.form' doesn't pass anything meaningful to the function. Just use the onclick like this:

onclick="setcases(document.contactForm);"

and it should work.

Cheers - Pit

Ashcrapper
06-03-2004, 10:27 AM
you truly are a superstar mate, that has worked perfectly! I cant tell you how much our database guy is going to thank you now that he wont have to go through tons of bizarre data submitted by wierd humans :D

thanks again, great forum. think ill come here more often!

Pittimann
06-03-2004, 10:35 AM
Hi!

You're welcome! :D

Greetz - also to the database guy ;) - Pit

Ashcrapper
06-03-2004, 10:45 AM
One last question :)

If I wanted to include some other form validation, how would I go about incorporating it all into one file?

Currently I was using this in an external script then using 'onsubmit' in the form tag to call it:


function formCheck()
{
returntype=false;

// check if NAME field is blank
if (contactForm.name.value == "")
{
alert("Please enter your name.");
contactForm.name.focus();
return (false);
}


// check if COUNTY field is blank
if (contactForm.county.value == "none")
{
alert("Please select the county you live in.");
contactForm.county.focus();
return (false);
}


// check if TEL field is blank
if (contactForm.tel.value == "")
{
alert("Please enter a valid telephone number so we can contact you.");
contactForm.tel.focus();
return (false);
}


// check if COURSE field is blank
if (contactForm.course.value == "")
{
alert("Please tell us the course you are currently studying.");
contactForm.course.focus();
return (false);
}


// check if COLLEGE field is blank
if (contactForm.placeOfStudy.value == "")
{
alert("Please tell us where you are currently studying.");
contactForm.placeOfStudy.focus();
return (false);
}


// check if NUS field is blank
if (contactForm.nus.value == "")
{
alert("Please enter your NUS number.");
contactForm.nus.focus();
return (false);
}

return (true);
}

//-->

Pittimann
06-03-2004, 11:08 AM
Hi!

Only a few things to be done:

1. The script tag for the external js has to be placed above "our" script.

2. In your validation function replace "return (true);" with these two lines:
setcases(document.contactForm);
document.contactForm.submit();

3. The anchor tag needs this:
<a ... href="#" onclick="return formCheck();return false;" ...><img ...></a>

That should do the job.

Cheers - Pit