473,486 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Form Field Validation Question

I have a form field on an ASP page and was wondering if I can require
the number that is entered to begin with a 0 or a 2. If the number
starts with anything else, the user is notified to update the number
correctly (Your [FormField] must begin with a 0 or a 2, Please update
your entry). Any code snip of this would be great

Feb 14 '06 #1
5 1088
VK

br*********@yahoo.com wrote:
I have a form field on an ASP page and was wondering if I can require
the number that is entered to begin with a 0 or a 2. If the number
starts with anything else, the user is notified to update the number
correctly (Your [FormField] must begin with a 0 or a 2, Please update
your entry). Any code snip of this would be great


While in the textfield, it is not a number, it is a string.

<input type="text" name="foo" onblur="
with (this) {
if ((value.chartAt(0)==0)||(value.charAt(0)==2)) {
alert('Bad boy!');
}
else {
alert('Good boy!');
}
}
">

For more complex checks a regexp would be more helpful.

Also welcome to move the check into the head section:
....onblur="myFunctionToCheck(this)"...

Feb 14 '06 #2
VK wrote:
br*********@yahoo.com wrote:
I have a form field on an ASP page and was wondering if I can require
the number that is entered to begin with a 0 or a 2. If the number
starts with anything else, the user is notified to update the number
correctly (Your [FormField] must begin with a 0 or a 2, Please update
your entry). Any code snip of this would be great
While in the textfield, it is not a number, it is a string.

<input type="text" name="foo" onblur="


type="text" is redundant, that is the default value for this attribute of
this element.
with (this) {
if ((value.chartAt(0)==0)||(value.charAt(0)==2)) { ^^^^^^^^^^^^^^^^ ^
string number

This works with implicit type conversion, so the following block is executed
for "0x0" and "0x2", too.

And it triggers the warning "deprecated `with' statement usage".
alert('Bad boy!');
}
else {
alert('Good boy!');
}
}
">

For more complex checks a regexp would be more helpful.


Not only for those:

if (this.value.test(/^[02]/))
{
//
}

The `onblur' event handler of the form control should not be used, but the
`onsubmit' event handler of the form.
PointedEars
Feb 14 '06 #3
Thanks for all your help.

Feb 15 '06 #4
VK

Thomas 'PointedEars' Lahn wrote:
warning "deprecated `with' statement usage".
Deprecated in what version? Thomas' JavaScript Light? :-).
I can understand if someone had a personal bad experience with "with"
statement. In such case I would understand the desire to share such
experience with the community (marking clearly the origin and the
reasons).
The `onblur' event handler of the form control should not be used, but the
`onsubmit' event handler of the form.


Only in the most primitive case when someone needs a form validation
before submission. Very often underlaying form elements' availability
and behavior determined by the previous input.

As the exact OP's situation is not stated clearly, your generalization
is not suitable.

Feb 15 '06 #5
On 15/02/2006 13:54, VK wrote:
Thomas 'PointedEars' Lahn wrote:
warning "deprecated `with' statement usage".


Deprecated in what version?


Thomas didn't say that it was deprecated in any particular version, did
he? No. It's /use/ is deprecated as it typically has little value and
does more to complicate code than improve it.

[snip]
The `onblur' event handler of the form control should not be used,
but the `onsubmit' event handler of the form.


Only in the most primitive case [...]


No, (almost) always. I would add that the change event should be used in
conjunction with the submit event, but the blur event should be avoided.
It inevitably leads to annoyance (or worse) when validation errors are
continually flagged.

This isn't new advice.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Feb 15 '06 #6

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

Similar topics

11
8722
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
6
4315
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
6
3066
by: CJM | last post by:
Can somebody clarify if/how/when a simple form is submitted when the <Enter> key is pressed? As I understood it, if you have a form with a single submit button, if enter is pressed, the form...
16
2188
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
2
3862
by: Kostas | last post by:
Just a quick question on this issue. Assume a small set of fixed values for a field. For instance Field Gender, values: Male, Female, Unknown If I create a Combo Box with the above values, I...
9
4153
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
11
2957
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
10
5683
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
5
3200
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
7
3577
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
0
6967
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
7132
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
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
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...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.