On Mar 9, 10:00 am, "agrath" <[EMAIL PROTECTED]> wrote:
> I have a requirement to validate input to a field as numeric only-
>
> A number of approaches could be used here, but I need to create 3
> different validators:
>
> 1) Currency
> 2) Numbers, including decimals and , i guess
> 3) Integers only
>
> I'm thinking an observer on the keydown event of the field I want to
> validate, the question is how do we validate the data being entered is
> correct and close all the loopholes.
Use keyup, not keydown. Use regular expressions, they are by far the
simplest and most robust way to validate such input. There is
everything you need to know here:
<URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP >
If you need help on the specifics, post to a group that specialises in
javascript:
news:comp.lang.javascript
<URL: http://groups.google.com.au/group/comp.lang.javascript >
> In a previous scenario such as this, I experienced major issues with
> users holding the shift key and typing number keys (to get symbols
> entered) as well as copy-and-paste being able to paste text data, so
> ended up needing to recheck and clear on-blur as well...
> The result was a massive script that was total bloatware for the
> operation.
Use a regular expression onkeyup and onsubmit.
> I was wondering how some of you might approach the problem.
>
> Prototype 1.5.0 is being used in the project.
>
> The way I see it, I can either force validation by only allowing
> numeric digits to be entered, and deny non numeric digits, or go with
That is very user-unfriendly and doesn't work anyway. There are more
ways of entering input that you can prevent, so don't try. Also,
users may type an incorrect character, then press delete, then start
typing again. If you deleted the character, they'll delete another
one... just warn users and get them to fix it themselves. You have
to do the validation again at the server anyway.
> a remember-the-milk type input validation, with an icon on the end of
> the field that indicates if the data is parsed correctly and prevent
> the form from being submitted unless all validation rules are met.
That is better, along with a message to say what is wrong (preferably
in the page, not an alert).
> Whilst I like the second option (validation icon) I think it will
> prove to be more trouble than it's worth.
It doesn't have to be. Remember that client-side validation is only a
convenience, it isn't at all reliable and all your validation has to
occur again on the server anyway.
You can organise a validation object, like:
var isValid = (function(){
var integerTest = /^\d+$/;
var decimalTest = /^\d+\.?\d*$/;
var currencyTest = /^\d+\.\d\d$/;
return {
integer: function(n){
return integerTest.test(n);
},
decimal: function(n){
return decimalTest.test(n);
},
currency: function(n){
return currencyTest.test(n);
}
};
})();
alert(
isValid.integer('123') // true
+'\n'+ isValid.integer('a123') // false
+'\n'+ isValid.decimal('1212.2232') // true
+'\n'+ isValid.currency('12.23') // true
+'\n'+ isValid.currency('1212.23') // true
+'\n'+ isValid.currency('1212.232') // false
);
Or if you feel brave, extend the String object's prototype:
String.prototype.isInteger = function(){
return /^\d+$/.test(this);
}
alert( '123'.isInteger() );
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---