By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

Changing Input Type, length and value

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <40**********************@news.rcn.com>, "Boyd Reilly"
<sp**********@spammersanonymous.com> 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
Jul 20 '05 #2

P: n/a
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" <hi************@aol.com> wrote in message
news:20***************************@mb-m25.aol.com...
In article <40**********************@news.rcn.com>, "Boyd Reilly"
<sp**********@spammersanonymous.com> 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. Theinput 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

Jul 20 '05 #3

P: n/a
"Boyd Reilly" <sp**********@spammersanonymous.com> wrote in message
news:40**********************@news.rcn.com...
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.
Jul 20 '05 #4

P: n/a
hi************@aol.com (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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
In article <br**********@hotpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com>
writes:
hi************@aol.com (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.


And since IE won't allow it to be *changed*, not written, then you can't
programatically change its type. Although I am very open to seeing code that
will allow this to work.
But I have never heard of a type="numeric".


It doesn't exist in HTML.


I already knew that.
--
Randy
Jul 20 '05 #6

P: n/a
On Sun, 11 Jan 2004 20:14:08 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> 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/

Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.