LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 09-22-2003, 03:55 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
Bug in Javascript


The alert code did not activate because the "temp" value is undefined.
The "temp" value suppose to contains either
document.form1.ip1.value or
document.form1.ip2.value or
document.form1.ip3.value or
document.form1.ip4.value

temp = document.form1.field_name.value;
alert ("temp contains " + temp); <--- THE PROBLEM IS HERE. SEE THE COMPLETE CODE BELOW.

====================================
Code:
<SCRIPT LANGUAGE="Javascript">
<!--

function only_numbers_2(ip)
 {
   var temp=new String();
   var temp2=""
   var count=0;
   var field_name;

   alert ("ip contains " + ip.name);
   if(ip.name == "ip1")
     field_name = "ip1";
   else
     if(ip.name == "ip2")
       field_name = "ip2";
   else
     if(ip.name == "ip3")
       field_name = "ip3";
   else
     if(ip.name == "ip4")
       field_name = "ip4";

   alert ("field_name contains " + field_name);

   if (!(event.keyCode == 48 || event.keyCode == 49 ||
         event.keyCode == 50 || event.keyCode == 51 ||
         event.keyCode == 52 || event.keyCode == 53 ||
         event.keyCode == 54 || event.keyCode == 55 ||
         event.keyCode == 56 || event.keyCode == 57 ||
         event.keyCode == 8))
     {
        alert("Only numeric is allowed");
        temp = document.form1.field_name.value; //preserve the current contents of text field
        alert ("temp contains " + temp);     <--- THE PROBLEM IS HERE
        document.form1.field_name.value="";
          while(count<temp.length)
           {
             if(temp.charAt(count) >= '0' && temp.charAt(count) <= '9')
               document.form1.field_name.value = document.form1.field_name.value + temp.charAt(count);
             count++;
           }
     }
 }

//-->
</SCRIPT>


<input type=text size=3 maxlength=3 name=ip1 onkeyup='only_numbers_2(document.form1.ip1)'><b>.</b>
<input type=text size=3 maxlength=3 name=ip2 onkeyup='only_numbers_2(document.form1.ip2)'><b>.</b>
<input type=text size=3 maxlength=3 name=ip3 onkeyup='only_numbers_2
(document.form1.ip3)'><b>.</b>
<input type=text size=3 maxlength=3 name=ip4 onkeyup='only_numbers_2
(document.form1.ip4)' >&nbsp;</td>
===========================================
 
Old 09-23-2003, 09:37 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
A couple of things:
1) You don't actually have a form called form1
2) I don't think you can sick variables in element names as far as I can remember it needs to be like:
temp = document.form1.[field_name]value;
 
Old 09-23-2003, 09:45 AM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Hi david_ross. Thank you for your help

temp = document.form1.[field_name]value; is correct.
 
  


Reply



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
Free86 bug or nVidia bug?? ProtoformX Linux - Software 2 05-12-2004 02:38 AM
javascript in <td>? akaash Programming 0 03-18-2004 12:22 AM
Bug in Javascript Linh Programming 1 10-14-2003 12:38 PM
Bug A Javascript that will not allowed alphabet character type into a text box Linh Programming 2 09-22-2003 11:15 AM
javascript?? Marci Sarwan Programming 1 01-18-2001 09:18 AM

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

All times are GMT -5. The time now is 07:44 PM.

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