Search code examples
reactjsregexvalidationdjango-serializer

How to print digit 5 or 6 in reactjs number validation


I have written code for Example TCA/2015/12345

validatetcanumber = e => {
    var constraint1 = (e.target.value.indexOf("TCA/") === 0);
    var constraint2 = (e.target.value.lastIndexOf("/") === 7);
    var constraint3 = false;
    var constraint4 = false;
    if (constraint1 && constraint2) {
        constraint3 = (e.target.value.slice().split("/")[1].length === 4);
        constraint4 = (e.target.value.slice().split("/")[2].length === 5);
    }
    if (constraint3 && constraint4)
        this.setState({ tca_number: e.target.value, tcaerror: '' });
    else
        this.setState({ tca_number: e.target.value, tcaerror: "Invalid Tca number" })
}

but I want to print two case either 5 or 6 digits for example 1. TCA/2015/12345 or TCA/2015/123456 2.TCA/2014/64587 or TCA/2014/645872

TCA/2015/5 digits or TCA/2015/6 digits

and below is my serializer code

check_tca_number = data['tca_number']
pattern = re.compile("^TCA/([0-9]{2,4})/([0-9]{5,6})$")
if not pattern.match(check_tca_number):
   raise serializers.ValidationError("TCA number is not valid")

Solution

  • I tried many difference method but just putting below code with in my main code and the problem is solved

    constraint4 = (e.target.value.slice().split("/")[2].length === 5) || (e.target.value.slice().split("/")[2].length === 6);