如何验证0到59的文本字段,它将允许1,2,3,* / 59

时间:2017-01-19 15:06:30

标签: javascript regex validation

我需要为此测试用例验证Textbox。

  1. 除*,/
  2. 外,不允许0到59个字符和特殊字符
  3. 它将允许1,2,3,4但1,2,3,60不允许
  4. 允许 / 59或1,2,3,4, / 59但1,2,3,5,* / 59/19不允许
  5. 我试过了,

    var input = document.getElementById('configMinute').value;      
    
                //console.info("Else Called");
                    var slashPattern = "/"; 
                    var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
                    var getStringCheck = checkSpecialChar(input,specialChars);
    
                    if(getStringCheck==true){   
    
                    // string = 1,2,3,*/10
                    // ,*/
    
                        var getStringValues = input.split(',');     
    
                        var notAllowedCharPattern = "<>@!#$%^&()_+[]{}?:;|'\"\\.~`-=";
    
                        var allowedChar = checkSpecialChar(input,notAllowedCharPattern);
    
                            if(allowedChar==false){ 
                            console.info(getStringValues);
    
                                getStringValues.forEach(function(element){  
                                //string = 1 2 3 */10
                                    var validateSlash = checkSpecialChar(element,slashPattern);     
    
                                    if(element.startsWith("*")==true){
                                        var newInput = element.split('/');  
                                        console.info("newInput: "+ element);
                                        newInput.forEach(function(element) {
                                            console.info("newInput Foreach: "+ element);
                                            if(element=='*' || (element>=0 && element <=59)){   
                                                return true;                    
                                            }
                                            else{
                                                alert("Please enter numbers between 0 to 59 or '*' ==>1");
                                                document.getElementById('configMinute').focus();
                                                return false;
    
                                            }
                                        });                         
    
                                    }else{
                                        console.info("* Else: "+ element);
                                        if(element=='*' || (element>=0 && element <=59)){   
                                            return true;                    
                                        }else{
                                            alert("Please enter numbers between 0 to 59 or '*' ==>1");
                                            document.getElementById('configMinute').focus();
                                            return false;
    
                                        }
                                    }               
    
                                }); 
                            }else{
                                alert ("File name has special characters \nAllowed Characters are *,/ ==>3");
                                document.getElementById('configMinute').focus();
                                return false;
                            }           
                    }else if(input == '*' || (input>=0 && input <=59)){ 
                        return true;        
                    }else{
                        alert("Please enter numbers between 0 to 59 or '*' ==>4");
                        document.getElementById('configMinute').focus();
                        return false;
                    }   
    

    提前致谢

1 个答案:

答案 0 :(得分:0)

你可以试试像

这样的正则表达式
data.frame()

它匹配行的开头((?:(?:^|,)(?:[1-5]?\d|\*?\/59)\b)+$ )或^,后跟数字0-59或,,可选地前面有/59。然后这个模式可以重复任意次,直到行结束。

See it here at regex101