1
我允许用户通过表单域输入框的尺寸。允许值是:涉及使用正则表达式的Javascript函数
- 正整数
- 必须以像素或百分比结束
样品有效的数字是300像素或70%
此外,我希望把一些额外的验证逻辑:
如果输入值以百分比表示,则该数字必须位于0> x < = 100的范围内
如果输入的数字是px,我希望能够检查硬编码的最小值和最大值。
我的正则表达式知识很少,因为我没用多年。
我觉得写一组辅助函数的这样的:
// returns true if value ends in px or % [with no spaces], false otherwise
function is_scale_valid(value){
}
//returns 'px', '%' or undefined
function get_scale_type(value){
}
// called after is_scale_valid, to ensure we are dealing with
// a +ve integer that falls in desired range. This function
// strips away the 'scale' part of the value and only looks
// at the number
function is_valid_number(value, scaletype, valid_min, valid_max){
}
谁能帮我填写这些辅助功能?
谢谢Cletus,我修改了代码并成功地测试了它 – 2010-02-14 14:14:19