I have a ‘Telephone’ text field in a simple form. I want to disable the
user to write anything other than numeric characters in this field. I am
aware that some international telephone numbers use alphabetical
characters.
Is there a simple way to do this like passing another parameter onto the
text field as a symbol or is it something more in depth?
Pale H. wrote:
I have a ‘Telephone’ text field in a simple form. I want to disable the
user to write anything other than numeric characters in this field. I am
aware that some international telephone numbers use alphabetical
characters.
Is there a simple way to do this like passing another parameter onto the
text field as a symbol or is it something more in depth?
Take a quick look at this tweet :
http://twitter.com/olabini/status/6269204813
Pale H. wrote:
I have a ‘Telephone’ text field in a simple form. I want to disable the
user to write anything other than numeric characters in this field. I am
aware that some international telephone numbers use alphabetical
characters.
Is there a simple way to do this like passing another parameter onto the
text field as a symbol or is it something more in depth?
Do you mean making it so that nothing happens when they type letters?
eg if i typed
123abc456
then i see
123456
If so then you could do something with javascript but i wouldn’t
recommend it. It would just confuse people. Better to do it with data
validation, so that they can type 123abc456 but when they submit the
form, the submit fails, and you highlight the phone number box with an
error message saying “please enter only numbers” or whatever. Rails
does this automatically when you have the
error_messages_for “myobject”
call included.
If so then you could do something with javascript but i wouldn’t
recommend it. It would just confuse people. Better to do it with data
validation, so that they can type 123abc456 but when they submit the
form, the submit fails, and you highlight the phone number box with an
error message saying “please enter only numbers” or whatever. Rails
does this automatically when you have the
error_messages_for “myobject” call included.
I’d prefer to use as little Javascript as possible; if the user has it
disabled, it can be problematic. Could I do some form of .include? to
validate the presense of alpha characters? Once I’ve done the
validation, I’ll redirect them back to the form with the parameters they
entered and display a flash[:notice].
Aldric G. wrote:
Pale H. wrote:
I have a ‘Telephone’ text field in a simple form. I want to disable the
user to write anything other than numeric characters in this field. I am
aware that some international telephone numbers use alphabetical
characters.
Is there a simple way to do this like passing another parameter onto the
text field as a symbol or is it something more in depth?
Take a quick look at this tweet :
http://twitter.com/olabini/status/6269204813
As stated in the original post: I am aware that some international
telephone numbers use alphabetical characters. I’m not concerned with
that as the form belongs to a very local business. I’d expect no less
than 99% of the businesses in this area to follow standard telephone
formats (Example: 01234 567890).
Pale H. wrote:
If so then you could do something with javascript but i wouldn’t
recommend it. It would just confuse people. Better to do it with data
validation, so that they can type 123abc456 but when they submit the
form, the submit fails, and you highlight the phone number box with an
error message saying “please enter only numbers” or whatever. Rails
does this automatically when you have the
error_messages_for “myobject” call included.
I’d prefer to use as little Javascript as possible; if the user has it
disabled, it can be problematic. Could I do some form of .include? to
validate the presense of alpha characters? Once I’ve done the
validation, I’ll redirect them back to the form with the parameters they
entered and display a flash[:notice].
The validation is easy
validates_format_of :phone, :with => /^[0-9]*$/, :message => “Please use
only numbers”
Note this allows empty strings, if you wanted to not allow empty strings
then user the :allow_blank or :allow_nil options.
http://railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001825&name=validates_format_of
Max W. wrote:
If so then you could do something with javascript but i wouldn’t
recommend it. It would just confuse people.
Even if you do this with JavaScript you must still do the validation and
error presentation. You cannot rely on JavaScript validation alone.
JavaScript can be easily disabled by the user, and many people use
plugins like no-script allowing JavaScript only on certain pages. They
would likely never know your JavaScript validation was even there.
I actually get very annoyed by JavaScript restrictive input control. So
much so I’ll often disable JavaScript just to avoid it. If you were
relying on that, the problem you’re trying to solve hits you on the
server-side, and that can hurt quite a lot.
Better to do it with data
validation, so that they can type 123abc456 but when they submit the
form, the submit fails, and you highlight the phone number box with an
error message saying “please enter only numbers” or whatever. Rails
does this automatically when you have the
error_messages_for “myobject”
+1
Not only is it better it’s necessary to prevent very ugly application
errors. For instance, if the table column is an integer type then you
would get an ugly database level exception. The end user would see the
generic “Something went horribly wrong!” page. This could severely
affect user experience and their opinion of your application.
I say, “Have a little more faith in your users!” Assume they are going
to type the values into the forms correctly, then validate that they
actually did. I don’t mean to say, “Trust their input.” Never do that!
Validate what they give you if there exists any chance for them to get
it wrong.
I’m saying trust that they can figure out how to enter the data
correctly by providing a good user interface, but not by imposing
restrictions on input.
The validation is easy
validates_format_of :phone, :with => /^[0-9]*$/, :message => “Please use
only numbers”
Note this allows empty strings, if you wanted to not allow empty strings
then user the :allow_blank or :allow_nil options.
http://railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001825&name=validates_format_of
Thanks, I appreciate your help.
Robert W. wrote:
I actually get very annoyed by JavaScript restrictive input control. So
much so I’ll often disable JavaScript just to avoid it. If you were
relying on that, the problem you’re trying to solve hits you on the
server-side, and that can hurt quite a lot.
Gosh, you mean people actually do this on their sites? I was speaking
hypothetically when i said you could do it …
Max W. wrote:
Pale H. wrote:
I have a ‘Telephone’ text field in a simple form. I want to disable the
user to write anything other than numeric characters in this field. I am
aware that some international telephone numbers use alphabetical
characters.
Is there a simple way to do this like passing another parameter onto the
text field as a symbol or is it something more in depth?
Do you mean making it so that nothing happens when they type letters?
eg if i typed
123abc456
then i see
123456
If so then you could do something with javascript but i wouldn’t
recommend it. It would just confuse people.
Agreed – and of course it wouldn’t work with JS off.
Better to do it with data
validation, so that they can type 123abc456 but when they submit the
form, the submit fails, and you highlight the phone number box with an
error message saying “please enter only numbers” or whatever. Rails
does this automatically when you have the
error_messages_for “myobject”
Better still: don’t reject it; just use gsub or something to remove the
characters you don’t want. It is extremely annoying to the user to be
told that he can’t enter a phone number any way he likes.
In other words, if I enter (518) 555-1212, 518/555.1212, or whatever,
your app should be smart enough to silently process that into 5185551212
(or whatever format is used internally) – and as a user, I shouldn’t
ever have to know or care!
Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
true, you could do that with a before_validation filter.
oh and on the subject, save phone numbers as strings not numbers!
Marnen Laibow-Koser wrote:
Better still: don’t reject it; just use gsub or something to remove the
characters you don’t want. It is extremely annoying to the user to be
told that he can’t enter a phone number any way he likes.
+1
And for those silly numbers that contain characters (i.e.
1-800-THE-NUMB) you could translate them to the actual digits
(18008436862) automatically.
oh and on the subject, save phone numbers as strings not numbers!
+1
There does come a point where you just have to put trust in people I
guess. Shame…
Max W. wrote:
true, you could do that with a before_validation filter.
oh and on the subject, save phone numbers as strings not numbers!
Max, thanks for your response. I am saving phone numbers as strings
anyway.
Marnen Laibow-Koser wrote:
In other words, if I enter (518) 555-1212, 518/555.1212, or whatever,
your app should be smart enough to silently process that into 5185551212
(or whatever format is used internally) – and as a user, I shouldn’t
ever have to know or care!
Couldn’t agree more Marnen, thanks. It will be smart enough.