In your match method put /^\d+$/ . Here, $ means “end of the line”, so any non-digit characters after the initial run of digits will cause the match to fail.
You can also use /\D/.test(z) . This operation tests the inverse of what you wait for. It returns true if the input has any non-numeric characters. Simply omit the ! and use if(/\D/.test(z))
Want to learn full stack development? Check out the full stack developer course from Intellipaat.