467,144 Members | 1,165 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,144 developers. It's quick & easy.

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
  • viewed: 967
Share:
5 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by CJM | last post: by
2 posts views Thread by Kostas | last post: by
9 posts views Thread by julie.siebel@gmail.com | last post: by
5 posts views Thread by lucyh3h@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.