Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Changing Input Type, length and value

Reply
Thread Tools

Changing Input Type, length and value

 
 
Boyd Reilly
Guest
Posts: n/a
 
      01-11-2004
I have a form that has the user pick the type of question he will answer.
The input field will be a text, numeric or date type. So, after the
question is answered, I need to change the input statement. This resembles
what I am doing:

<form name="frm1">
Step 1 - Choose a File: <select name="selA" OnChange="makeinputbox;"
width="200" style="width:200">
<option value="">Date of Birth</option>
<option value="MASTER">Age</option>
<option value="ADDRESS3">Maiden Name</option>
Step 2 - Insert Value: <INPUT TYPE="text" NAME="MySingleLineTextBox"
SIZE=50 MAXLENGTH=75 VALUE="">

</select>
</form>
<script language="JavaScript">

<!--
function makeinputbox {
document.frm1.MySingleLineTextBox.TYPE='numeric';
document.frm1.MySingleLineTextBox.SIZE=5;
document.frm1.MySingleLineTextBox.MAXLENGTH=5;
document.frm1.MySingleLineTextBox.value=1.00;
}
// -->

</script>

The only thing that happens is that the value gets set to 1, not 1.00. The
input bo still keeps all of its original characteristics.

Any ideas what I need to do?

TIA.

Boyd





 
Reply With Quote
 
 
 
 
Boyd Reilly
Guest
Posts: n/a
 
      01-11-2004
Randy:

Thanks for he help. If you haven't already noticed, I'm a bit new to java
scripting. If you could, please explain the difference between <script
language="JavaScript"> and <script language="text/javascript">.

Also, I have been learning java scripting, mostly, through example. You say
type is read-only. Is there a way to change it to a date or numeric type
format? Basically, I want to restrict users to only a numbers or a date
format. I guess if it is really a read-only function I'm going to have to
develop a different method of attacking this problem - suggestions are
welcome.

TIA.

Boyd


"HikksNotAtHome" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In article <4000cd7d$0$6744$(E-Mail Removed)>, "Boyd Reilly"
> <(E-Mail Removed)> writes:
>
> ><script language="JavaScript">

>
> use type="text/javascript" instead.
>
> >
> ><!--
> >function makeinputbox {
> >document.frm1.MySingleLineTextBox.TYPE='numeric ';

>
> You can't programatically change the type of an input. Its a read only

value.
> But I have never heard of a type="numeric".
>
> >document.frm1.MySingleLineTextBox.SIZE=5;

>
> document.frm1.mySingleLineTextBox.size = 5;
>
> Javascript is case sensitive.
>
> >document.frm1.MySingleLineTextBox.MAXLENGTH=5;

>
> Should be:
>
> document.frm1.MySingleLineTextBox.maxLength=5;
>
> >document.frm1.MySingleLineTextBox.value=1.00;

>
> document.frm1.MySingleLineTextBox.value="1.00";
>
> as you have it written, its setting it to the number 1.00, which it

displays as
> 1, quote it and make it a string, it displays as 1.00
>
> >}
> >// -->
> >
> ></script>
> >
> >The only thing that happens is that the value gets set to 1, not 1.00.

The
> >input bo still keeps all of its original characteristics.

>
> The case is what was causing it not to work, as the properties you are

trying
> to change are case Sensitive. Other than the type, which is readonly.
>
> But, when you say you want to change it to Numeric, Date or Text. Of those
> three, only the text is a valid type of input. You are wanting to limit

the
> type of text that you will accept and thats a different story. Search the
> comp.lang.javascript archives on how to validate a Number and how to

validate a
> Date (its not easy).
>
> Hope this helps.
> --
> Randy



 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      01-11-2004
"Boyd Reilly" <(E-Mail Removed)> wrote in message
news:4000ed64$0$6736$(E-Mail Removed)...
>Thanks for he help. If you haven't already noticed, I'm a bit
>new to java scripting.


And Usenet too by the look of it.

JavaScript (one word) is not scripting with Java. Java is a distinct
language (and in most respects a very different language). The name was
probably a mistake as it causes a lot of confusion, but it is too late
now.

There is a longstanding convention on Usenet that messages are formatted
so that only material that is being responded to is quoted and the text
that represents the response appears under the quoted text that is being
responded to. You have quoted the entire message you are responding to
verbatim (and badly line wrapped) and have placed your reply above it. A
practice known as "Top Posting" and strongly discouraged.

It is often proposed that there is a correlation between top posting and
evidence in the top posted text of a failure to read the material that
has been posted over.

Another Usenet convention has technical groups maintaining a frequently
asked questions resource and asking new participants to read it,
preferably prior to posting to the group but ASAP otherwise. The
comp.lang.javascript FAQ is posted to the group on a regular bases
(search for FAQ in subject lines) and is also available online at:-

<URL: http://jibbering.com/faq/ >

Section 2.3 is of immediate relevance, but reading the rest (and linked
resources) will be invaluable for a novice JavaScript author.

>If you could, please explain the
>difference between <script language="JavaScript"> and


Randy actually wrote - type="text/javascript" - rather than:-

><script language="text/javascript">.


A discrepancy that you probably would have spotted if you had posted
that line directly under a quote of his comment.

The HTML 4.01 SCRIPT element has 7 official attributes defined in its
(loose) DTD. TYPE and LANGUAGE are two of those. The TYPE attribute is
listed as required by the DTD and so it must be present for an HTML 4
document to be valid. If the script language used is JavaScript, and
baring in mind that HTML attribute names are case insensitive, then the
opening script tag must include:-

type="text/javascript"

On the other hand the LANGUAGE attribute is deprecated, which is
intended to indicate that it should no longer be used. And the HTML 4.01
strict DTD does not include the LANGUAGE attribute so a document written
to conform to that DTD could not include it and still be valid.

In practice these are considerations of formal correctness. Most
browsers don't know any other scripting languages and those that do
default to assuming JavaScript in the absence of anything to the
contrary. Browsers are also extremely tolerant of errors and willing to
bend over backwards to make some sort of sense out of whatever rubbish
they are sent, so just writing "<script>" will probably have the desired
result.

However, there is an axiom that goes: be tolerant in what you accept and
strict in what you send. The browsers are tolerant in what they accept
and it is up to the web authors to be strict in what they send. Which
means valid HTML (at least) and so the TYPE attribute in script tags and
only the LANGUAGE attribute if the strict DTD is not used (and even then
preferably no LANGUAGE attribute).

But valid HTML has an advantage of its own when combined with
JavaScript. The script wants to be able to interact with a DOM
constructed from that HTML and there are (more or less) well specified
rules as to what that DOM should be like when it is constructed based on
valid HTML. The browsers might not always manage to hit the desired
target but at least you know that they are all shooting at the same
target.

While a browser receiving invalid HTML will apply various "error
correcting" rules to sort the HTML out into a DOM that makes sense. But
those rules are particular to the individual browsers and not published
or standardised in any way so there can be a lot of variation in the DOM
that results. The last thing a script author want is to encourage more
inconsistency in the DOMs that they will be working with (there is
enough of that already).

>Also, I have been learning java scripting, mostly, through example.


Be extremely cautions of example scripts found on the internet
(especially in the form of copy'n'paste collections) you will probably
find considerably more examples that exemplify how *not* to write
scripts than good examples.

>You say type is read-only. Is there a way to change
>it to a date or numeric type format?


There is no such thing as date or numeric format with INPUT elements.
They accept strings of text, nothing more and nothing less. You can
specify a maximum length for the string but that is it.

>Basically, I want to restrict users to only a numbers or a date
>format.


No, that is not what you want to do. You want to provide the user with
information about the format you would like, possibly you would also
want to verify that what the user enters is in that format once they
have entered it and prior to sending the information to a server for
back-end processing (baring in mind that all client-side verification
can be subverted so the back-end must always check that its input
conforms to its requirements). That is standard form validation and you
will be able to find hundreds of examples in the archives. Pay
particular attention to Regular Expression based validation code (but
not for all tasks).

>I guess if it is really a read-only function I'm going to have to
>develop a different method of attacking this problem - suggestions
>are welcome.


And Randy wrote:-

<snip>
>>.... You are wanting to limit the type of text that
>>you will accept and thats a different story. Search the
>>comp.lang.javascript archives on how to validate a Number

>and how to validate a Date (its not easy).

<snip>

The comp.lang.javascript archives are available at groups.google.com and
google provide more ways of searching them than you will probably find a
use for.

Richard.


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      01-11-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (HikksNotAtHome) writes:

> You can't programatically change the type of an input. Its a read only value.


Not in DOM 2.
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-6043025>

It is read-only in DOM 1, but since you need to be able to set the type
of a newly created input element (document.creatElement("input")), it
must be writable.

Browser uspport for DOM 2 is a different problem, ofcourse. As usual
IE won't understand it when you change the type after setting it the
first time.

> But I have never heard of a type="numeric".


It doesn't exist in HTML.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Jim Ley
Guest
Posts: n/a
 
      01-13-2004
On Sun, 11 Jan 2004 20:14:08 +0100, Lasse Reichstein Nielsen
<(E-Mail Removed)> wrote:

>Browser uspport for DOM 2 is a different problem, ofcourse. As usual
>IE won't understand it when you change the type after setting it the
>first time.


document.implementation.hasFeature("DOM 2.0") makes no claim to
supporting DOM 2, so you can't complain it doesn't do something it
claims to.

(Of course the hasFeature test is ridiculous)

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass input Array value to function to calc different input value Susan Cranford Javascript 2 07-05-2005 02:53 AM
left(string, length) or right(string, length)? Sam ASP .Net 3 02-17-2005 12:01 PM
System.ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length =?Utf-8?B?SG96aQ==?= ASP .Net 1 06-01-2004 11:06 PM
How to get length of string? length() problems Mitchua Perl 5 07-17-2003 12:08 AM
String.value and String.value.length doesn't works together in IE with JVM Wee Javascript 1 07-16-2003 12:32 PM



Advertisments