LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2003, 12:52 PM   #1
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
JavaScript Question


I have a simple? problem.

I have a form where a user can view his personal details, and update them if necessary.
For example, the first row of a table would look something like this:

Name: Jack Smash

where "Jack Smash" is the value of a text field.
Since I'm using a text field, a user can simply overwrite his data and click submit to submit the new data.

The only problem is, changing the data adds a whole ton of white space to it.
For example, if I changes "Jack Smash" to "Joel", my string length is something like 26 - which makes it a pain for error checking.

So I guess my simple question is this, how do I trim a sting in JavaScript? It doesn't seem like there is any function to do this.

Any ideas would be greatly appreciated.

Thanks!
 
Old 12-23-2003, 01:59 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
So long as you check the length of the input field value it should be ok.
 
Old 12-23-2003, 02:16 PM   #3
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by david_ross
So long as you check the length of the input field value it should be ok.
I'm not quite sure what you mean by that?

Here's sample code:

<input type="text" value=$name name="new_name"
size="30" />

If, for example, the value of $name is "Jack", the size of "new_name" in the JavaScript function will be "30", but I want it to be "4".

Sorry if I'm not making sense!
 
Old 12-23-2003, 02:29 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
I'm still not seeing your problem. Try this sample code:
Code:
<HTML>
<HEAD>
<TITLE>Length of input value test page</TITLE>
<SCRIPT language="javascript">
function chklen(){
alert(document.myform.new_name.value.length);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform">
<input type="text" value="Jack" name="new_name" size="30">
<input type="button" value="Check Length" onclick="javascript:chklen()">
</FORM>
</BODY>
</HTML>
Perhaps it is a browser issue. What are you using?
 
Old 12-23-2003, 02:42 PM   #5
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Ok, when I execute something like what you have - it gives me the field length, not the length of the value I want.

But I just figured something out, I'm getting the values from a Table (I'm using PostgreSQL), and I think the size of the field in the table is now becoming the size of the value in the text field.

I suppose I could put some PHP code in before the form and trim all of the variables.

Does this make sense?
 
Old 12-23-2003, 03:00 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Sorry I think I'm missing something. What javascript function are you using? And if you don;t want ti to return the length of the field, what do you want it to return.
 
Old 12-23-2003, 04:49 PM   #7
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Dave,
I thought I would just give you my code.

Remember that all the fields in the form are initialized to the current values from the database.
If a user wishes to change a field, he simply overwrites what is in the form.

If you look at the JavaScript, I simply want to make sure none of the fields are left empty, so I check to make sure none of the string lengths are zero.
However, the strings length never is zero (this is my problem), even if I delete what is in the form.

Sorry if the code is a bit messy.

<!-- This page allows Students to update/modify their
preliminary information -->

<html>
<head><title>Student Preliminary Info</title>

<script = "JavaScript">

function verifyInfo ( ) {

var addr = document.stdupd.new_addr.value;
var post = document.stdupd.new_post.value;
var city = document.stdupd.new_city.value;
var prov = document.stdupd.new_prov.value;
var email = document.stdupd.new_email.value;

// this if never executes!!!

if ( addr == "" ) {
alert ("You must have an address");
return false;
}

}
</script>
<link rel="stylesheet" type="text/css" href="css/admin.css" />
</head>

<body style="background-color: silver">

<table width="70%" border="1" align="center"
style="background-color: white; border-color: black"
cellpadding = "3">
<tr>
<th style="background-color: blue">
<p><span style="color: white">
<br/><h1>Your Preliminary Info</h1>
</span></p></th>
</tr>
<tr>
<td><br/><p><h3 align="center">This is your current
information:</h3><br/></p>
<p align="center">

<?php
$conn = pg_connect("dbname=*****
user=******
password=*****")
or die("No connection");

$sql = "SELECT * FROM student
WHERE id='$username'";
or die("Cannot connect to student table");

$data = pg_fetch_array($result,0);
$name = $data["sname"];
$sno = $data["sno"];
$addr = $data["addr"];
$post = $data["postal"];
$city = $data["city"];
$prov = $data["prov"];
$email= $data["email"];

echo "<form method='post' name='stdupd'
action='verstdupdate.php4?user=$username'>";

echo "<table border='1' cellspacing='0'
style='background-color:silver'
cellpadding='3'><tr>";
echo "<td align='right'><b>Name:</b></td>";
echo "<td><b>$name</b></td></tr>";
echo "<tr><td align='right'><b>Address:</b></td>";
echo "<td><input type='text' size='0'
name='new_addr' value='$addr'/></td></tr>";
echo "<tr><td align='right'><b>Postal Code:</b></td>";
echo "<td><input type='text' size='30'
name='new_post' value='$post'/></td></tr>";
echo "<tr><td align='right'><b>City:</b></td>";
echo "<td><input type='text' size='30'
name='new_city' value='$city'/></td></tr>";
echo "<tr><td align='right'><b>Province:</b></td>";
echo "<td><input type='text' size='30'
name='new_prov' value='$prov'/></td></tr>";
echo "<tr><td align='right'><b>Email:</b></td>";
echo "<td><input type='text' size='30'
name='new_email' value='$email'/></td></tr>";
echo "</table>";

echo "<br/><input type='submit' value='Update'
onClick='return verifyInfo()' />";


echo "</form>";
 
Old 12-23-2003, 05:12 PM   #8
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Then why not change the javascipt to:
Code:
function verifyInfo ( ) {
if(document.stdupd.new_addr.value.length < 1){alert("You must specify a postal address")}
if(document.stdupd.new_post.value.length < 1){alert("You must specify a post")}
if(document.stdupd.new_city.value.length < 1){alert("You must specify a city")}
if(document.stdupd.new_prov.value.length < 1){alert("You must specify a providence")}
if(document.stdupd.new_email.value.length < 1){alert("You must specify an e-mail address")}
}

Last edited by david_ross; 12-23-2003 at 05:13 PM.
 
Old 12-23-2003, 06:01 PM   #9
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
I think I'm not explaining myself clearly!
If I erase what is on the form - the length does NOT go to zero. So the JavaScript doesn't help.

But I got around it by doing this in the php:

$name = trim($name);

which got rid of the extra whitespace from the database field.
Now the JavaScript works.

Thanks for your help - sorry to be a pain!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question python javascript. moby Programming 2 07-16-2004 11:55 AM
He, ANOTHER javascript question Squall Programming 3 01-27-2004 07:10 PM
Javascript question unimaginative Programming 3 01-26-2004 03:32 PM
Javascript question? mikeshn Programming 2 08-22-2002 04:44 PM
JavaScript Question vance Programming 2 04-14-2002 01:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration