My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Allow Malaysian Mobile Number In Textbox Using React

Buang Hosen's photo
Buang Hosen
·Apr 10, 2020

Hi buddies!!!

Im still learning using React library. Please I really need your help, someone may teach me how to allow only phone number in textbox?

Currently I was using regex to allow only number but i cant change the regex format to accept for particular country phone number.

With this I include with a partial of codes. Any help I would like to appreciated so much. Have a nice day :D

p/s : I need to change it to Malaysia phone number format.

    <CustomInput
        id="phone-no"
        formControlProps={{
            fullWidth: true
        }}
        inputProps={{
            value: state.memberLoginInformation.phone,
            type: "text",
            onChange: event => {
                const re = /^[0-9\b]/;
                // if value is not blank, then test the regex
                if (event.target.value === '' || re.test(event.target.value))
                {
                    //    this.setState({value: event.target.value})

                    const { memberLoginInformation } = state;
                    setState({
                        ...state,
                        memberLoginInformation: {
                        ...memberLoginInformation,
                        phone: event.target.value
                        }
                    });
                }
            },
            onBlur: () => {
                const { memberLoginInformation } = state;
                setState({
                    ...state,
                    memberLoginInformation: {
                    ...memberLoginInformation,
                    isPositionPrestine: false
                    }
                });
            }
        }}
    />