Form Field Validation Question

B

bravesplace

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
 
V

VK

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)"...
 
T

Thomas 'PointedEars' Lahn

VK said:
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
 
V

VK

Thomas said:
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.
 
M

Michael Winter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top