473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting a default value in forms and protecting it

How do you create a text input box, which shows a default value of 1+
and blocks the user from deleting it? Thanks so much! :))

Oct 13 '06 #1
8 1619

hanseym...@gmail.com wrote:
How do you create a text input box, which shows a default value of 1+
and blocks the user from deleting it? Thanks so much! :))
If you want a text field not editable by user a.k.a. read-only, you
should use respective attribute:

<input type="text" name="super_field" value="1+" readonly="readonly" />

(this is an xhtml syntax)

If you want your form having a parameter but it is not necessary to
show it to the user it's appropriate to use a hidden field:

<input type="hidden" name="...." value="..." />

Oct 13 '06 #2

vy********@gmail.com wrote:
hanseym...@gmail.com wrote:
How do you create a text input box, which shows a default value of 1+
and blocks the user from deleting it? Thanks so much! :))

If you want a text field not editable by user a.k.a. read-only, you
should use respective attribute:

<input type="text" name="super_field" value="1+" readonly="readonly" />

(this is an xhtml syntax)

If you want your form having a parameter but it is not necessary to
show it to the user it's appropriate to use a hidden field:

<input type="hidden" name="...." value="..." />
Is it possible to have your cake and eat it too, that is have a text
input that works as a input, displaying 1+, and allowing the user to
input more, while not allowing 1+ to be deleted? Is this beyond the
limits of JS?

Oct 13 '06 #3
in********@gmail.com wrote:
Is it possible to have your cake and eat it too, that is have a text
input that works as a input, displaying 1+, and allowing the user to
input more, while not allowing 1+ to be deleted? Is this beyond the
limits of JS?
You could create 2 elements right next to each other; one just static
text displaying "1+' and the other an actual text input field; carefully
set all the colors, margins, and borders so the user doesn't see the
transition between them. Then when the form is submitted, add the "1+'
to the text value using JS.
Oct 13 '06 #4
ha********@gmail.com wrote:
How do you create a text input box, which shows a default value of 1+
and blocks the user from deleting it? Thanks so much! :))

<input type="text" value="1+" readonly>

Danny
Oct 14 '06 #5

in********@gmail.com wrote:
vy********@gmail.com wrote:
hanseym...@gmail.com wrote:
How do you create a text input box, which shows a default value of 1+
and blocks the user from deleting it? Thanks so much! :))
If you want a text field not editable by user a.k.a. read-only, you
should use respective attribute:

<input type="text" name="super_field" value="1+" readonly="readonly" />

(this is an xhtml syntax)

If you want your form having a parameter but it is not necessary to
show it to the user it's appropriate to use a hidden field:

<input type="hidden" name="...." value="..." />

Is it possible to have your cake and eat it too, that is have a text
input that works as a input, displaying 1+, and allowing the user to
input more, while not allowing 1+ to be deleted? Is this beyond the
limits of JS?
You can use the onchange event handler for this.
http://msdn.microsoft.com/workshop/a...s/onchange.asp

Essentially apply the onchange handler to the form element, make the
handler a function which checks of the user has added text to the form,
or subtracted it, something like:

function checkFormValue(formElement)
{
if(formElement.value.length < 3)
{
formElement.value = "1 + ";
}
}

You can apply this to the form element many ways, the easiest would be
to do:
<input type="text" name="addMoreToMe" id="addMoreToMe" value="1 +"
onchange="checkFormValue(this);" />

Essentially what this does is checks if the length of the form is less
than three, reset the value to the original value. This is a pretty
basic example, as there are ways around it, so be sure to validate the
form properly, but I believe it should be a start for you.

One of the ways around this simple example would be someone pasting
text over the form value, the onchange event handler would never be
fired before the value is less than three as long as whatever they
paste into the form is a string with a length greater than three
(characters.) For example if the form is there, with the "1 +" in it,
and I select the "1 +" and paste "hsdhuhfsfdufh" over it, the above
function I wrote will not correct it.

Oct 15 '06 #6
On Oct 15, 3:41 am, "John Postlethwait" <john.postlethw...@gmail.com>
wrote:

<snip>
Essentially apply the onchange handler to the form element, make the
handler a function which checks of the user has added text to the form,
or subtracted it, something like:

function checkFormValue(formElement)
{
if(formElement.value.length < 3)
{
formElement.value = "1 + ";
}
}
Check the first two to four characters to see if they match 1+, 1 +, or
1+ , and then add it back.....

Don't make it harder than it has to be :)

But, your function doesn't work the way you think it would with regards
to what the OP asked. Test it. Type in "Humpty Dumpty" and see what
gets changed...
--
Randy

Oct 15 '06 #7

One Dumm Hikk wrote:
On Oct 15, 3:41 am, "John Postlethwait" <john.postlethw...@gmail.com>
wrote:

<snip>
Essentially apply the onchange handler to the form element, make the
handler a function which checks of the user has added text to the form,
or subtracted it, something like:

function checkFormValue(formElement)
{
if(formElement.value.length < 3)
{
formElement.value = "1 + ";
}
}

Check the first two to four characters to see if they match 1+, 1 +, or
1+ , and then add it back.....

Don't make it harder than it has to be :)

But, your function doesn't work the way you think it would with regards
to what the OP asked. Test it. Type in "Humpty Dumpty" and see what
gets changed...
--
Randy
My mistake, the "onchange" handler doesn't work as I described... On
firefox the event is not fired until the form element loses focus, or
blurs. Changing it to "onkeypress" in the above example I gave works
well in Firefox, unfortunately it doesnt work in IE so you will have to
use keydown instead on IE.

I also agree with your solution. Checking to ensure the first parts of
the string in the form have 1+ or something similar is a better way to
go.

Oct 18 '06 #8
myTextElement.onkeypress = function() {
if(!this.value.match(/^1\+/)) this.value = '1+' + this.value;
}

this will check if field value stars with '1+' on every key pressed.
Typing though is complicated.

The question is why this behaviour is needed.

Oct 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
10
by: MLH | last post by:
I have an A97 table with a Yes/No field named TowJob and a form bound to that table. The TowJob control on the form is bound to the same field. It is an option group with Yes and No bttns valued...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
3
by: countd4 | last post by:
I have built a working user control. However, to make it work, I always have to set certian properties using the properties sheet for the control when using it on other forms. I want to be able to...
6
by: Rick | last post by:
I have a main form called frmDemo with a subform frmStageTrack_Sub. On the subform is a Combo Box CboTermID. The frmDemo is tied to tblDemo and the frmStageTrack_Sub is tied to tblStageTrack. ...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
7
by: Academic | last post by:
What are the different effects of the following two statements: C1.Cursor = Cursors.WaitCursor C1.Cursor.Current = Cursors.WaitCursor I believe the first replaces the entire C1.Cursor...
8
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.