Regular Expression for alphanumeric and underscores

前端 未结 20 1082
北荒
北荒 2020-11-22 10:01

I would like to have a regular expression that checks if a string contains only upper and lowercase letters, numbers, and underscores.

20条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:57

    Required Format Allow these 3:

    1. 0142171547295
    2. 014-2171547295
    3. 123abc

    Don't Allow other formats:

    validatePnrAndTicketNumber(){
        let alphaNumericRegex=/^[a-zA-Z0-9]*$/;
        let numericRegex=/^[0-9]*$/;
        let numericdashRegex=/^(([1-9]{3})\-?([0-9]{10}))$/;
       this.currBookingRefValue = this.requestForm.controls["bookingReference"].value;
       if(this.currBookingRefValue.length == 14 && this.currBookingRefValue.match(numericdashRegex)){
         this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
       }else if(this.currBookingRefValue.length ==6 && this.currBookingRefValue.match(alphaNumericRegex)){
        this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
       }else if(this.currBookingRefValue.length ==13 && this.currBookingRefValue.match(numericRegex) ){
        this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
       }else{
        this.requestForm.controls["bookingReference"].setErrors({'pattern': true});
       }
    }
    
    
    

提交回复
热议问题