Jump to content

Capitalize Form Names Help Please


paulmo

Recommended Posts

this used to work for me. at a loss; thanks for help.

<script type="text/javascript">var FormName = "myform";var FieldName = "name";var FieldName = "message";function CapitalizeNames() {var ValueString = new String();eval('ValueString=document.'+FormName+'.'+FieldName+'.value');ValueString = ValueString.replace(/ +/g,' ');var names = ValueString.split(' ');for(var i = 0; i < names.length; i++) {   if(names[i].length > 1) {	   names[i] = names[i].toLowerCase();	   letters = names[i].split('');   	letters[0] = letters[0].toUpperCase();	   names[i] = letters.join('');	   }	else { names[i] = names[i].toUpperCase(); }	}ValueString = names.join(' ');eval('document.'+FormName+'.'+FieldName+'.value=ValueString');return true;}</script></head><div id="col1"><div style="background-color:#FFFFFF; padding:10px" class="rounded {10px}"><p><form name="myform" method="post" action="beta.php"> //post to same page<p>First Name: <input type="text" name="name" class="buttonsb"/></p<center>Write something: </br></br><input type="text" name="message" class="buttonsb"/> <input type="hidden" name="searching" value="yes" /></br></br></br><input type="submit" value="Process" class="buttons" onclick="return CapitalizeNames()"></center></form>

Link to comment
Share on other sites

Hmm, this code isn't really efficient and has a few problems:1. You're declaring the same variable twice. This will give an error because the variable is already declared, aside from that, if it didn't give an error, you'd be changing the value of the variable.

var FieldName = "name";var FieldName = "message";

2. Instead of using eval(), use the better methods to access elements: getElementById().3. I could fix your Javascript for you, but I actually recommend that you do this on the server side, since users can turn off Javascript. So I'll give you a PHP solution instead.Here's what you should do in beta.php

$name = $_POST['name'];$message = $_POST['message'];$name = ucfirst($name);$message = ucfirst($message);

Link to comment
Share on other sites

yes! thanks for that! :) is this in the php manual? if so i missed it. i'm also trying full text search from text form input for any word(s) (question posted on php forum), and been reading about regex. any direction or links to reading appreciated.have read peachpit press' books on php and javascript; seems so many tasks can be covered by both. but i'd rather use php when possible so thanks a lot for this!

Link to comment
Share on other sites

In a way, Regular Expressions are a language of their own. It can take years of steady practice to really know what you're doing. Fortunately, simple matches are easy to construct, and you can increase complexity as time and needs require. In that way, it's not like a language, where you have to learn a whole bunch just to get started.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...