score:16
i have restricted the textbox to allow only numbers and formatted the mobile number as us mobile number format. follow the below code.
handlechange(e) {
const onlynums = e.target.value.replace(/[^0-9]/g, '');
if (onlynums.length < 10) {
this.setstate({ value: onlynums });
} else if (onlynums.length === 10) {
const number = onlynums.replace(
/(\d{3})(\d{3})(\d{4})/,
'($1) $2-$3'
);
this.setstate({ value: number });
}
}
score:-1
set class on your textfield:
<textfield type="number"
inputprops={{classname:'digitsonly'}}
floatinglabeltext="mobilenumber"
value={this.state.value}
onchange={this.handlechange}
id={"mobilenumber"} name={"mobilenumber"}
/>
you can apply a class on your textfield that enables only numbers as follows:
$(".digitsonly").on('keypress',function (event) {
var keynum
if(window.event) {// ie8 and earlier
keynum = event.keycode;
} else if(event.which) { // ie9/firefox/chrome/opera/safari
keynum = event.which;
} else {
keynum = 0;
}
if(keynum === 8 || keynum === 0 || keynum === 9) {
return;
}
if(keynum < 46 || keynum > 57 || keynum === 47) {
event.preventdefault();
} // prevent all special characters except decimal point
}
restrict paste and drag-drop on your textfield:
$(".digitsonly").on('paste drop',function (event) {
let temp=''
if(event.type==='drop') {
temp =$("#mobilenumber").val()+event.originalevent.datatransfer.getdata('text');
var regex = new regexp(/(^100(\.0{1,2})?$)|(^([1-9]([0-9])?|0)(\.[0-9]{1,2})?$)/g); //allows only digits to be drag and dropped
if (!regex.test(temp)) {
event.preventdefault();
return false;
}
} else if(event.type==='paste') {
temp=$("#mobilenumber").val()+event.originalevent.clipboarddata.getdata('text')
var regex = new regexp(/(^100(\.0{1,2})?$)|(^([1-9]([0-9])?|0)(\.[0-9]{1,2})?$)/g); //allows only digits to be pasted
if (!regex.test(temp)) {
event.preventdefault();
return false;
}
}
}
call these events in componentdidmount() to apply the class as soon as the page loads.
score:3
in case you are using a form-validation library like react-hook-form, you can validate your textfield like this,
<textfield
type="number"
{...register("phonenum",{
required: {
value: true,
message: 'please fill this field',
},
pattern: {
value: /^[1-9]\d*(\d+)?$/i,
message: 'please enter an integer',
},
min: {
value: 1,
message: 'value should be atleast 1',
},
})}
error={errors?.index ? true : false}
helpertext={errors?.index?.message}
/>
in case, you want to input phone numbers only, i would highly recommend to consider using react-phone-input-2.
score:3
its really simple in mui just add the line
<textfield
type='number' //ad this line
onchange={(e) => setcode(e.target.value)} label="enter otp" />
score:12
import input from '@material-ui/core/input';
use <input type="number" />
instead.
Source: stackoverflow.com
Related Query
- How to allow only numbers in textbox and format as US mobile number format in react js? ex : (224) - 5623 -2365
- How to allow only numbers in textbox in reactjs?
- How can I make my input to be only limited to a maximum of 10 numbers and don't allow any characters?
- How to do so that my field must at least be able to contain only numbers and a + in front of the number (if the user wishes)
- Allow chinese characters and English numbers only in textbox - Javascript
- How to only allow a specific element type as children using Flow and React?
- How can I format my CSS and Bootstrap so that only certain elements wrap when page width decreases?
- How to allow only numbers on an input in react hooks?
- How to allow german user to enter numbers in german format in React JavaScript
- How do I get my HTML form date input to only allow specific days of the week to be chosen using JavaScript, React and HTML?
- How to input decimal numbers and auto format with thousand separators at the same time in javascript?
- How to unmask US mobile number format while making an API call in react js?
- How to display in input textbox of credit card number the 4 numbers then space?
- How do I prevent user from entering a username that starts with a number, but allow numbers only after alpha characters?
- How can I only allow certain components to scroll and keep certain React components fixed?
- How to limit number with 0-999 and just three numbers after the comma in React
- With redux form how can I normalize and allow only positive integers on an input type number?
- Regex only allow whole and decimal point numbers
- How do I format AntD form input fields such as phone numbers and SSNs in React?
- Allow textbox only digits and first digit starts with 2 to9
- How to store images securely and allow access only for user [React]
- How to allow only double numbers in input React js
- How can I make a text box in React which allows only numbers or an empty value, which triggers the number keypad on mobile?
- how to remove number from array of numbers using TypeScript and React
- Input box onchange only allow numbers and one dot
- React - how to capture only parent's onClick event and not children
- In React, how to format a number with commas?
- How to parse ISO 8601 into date and time format using Moment js in Javascript?
- How to wait for AJAX response and only after that render the component?
- Only allow specific components as children in React and Typescript
More Query from same tag
- React: storing components in an object
- How to override css and import with another component in react js
- How can I make use of Error boundaries in functional React components?
- Not sure what to put in callback function for routing with express and react front end
- How to declare a function type as a parameter in TypeScript
- Cant remove padding-bottom from Card Content in Material UI
- Error: Cannot read property 'function' of undefined
- React app works on Chrome, but not Firefox
- Method “props” is only meant to be run on a single node. 0 found instead. Enzyme -Error
- Gatsby Config Not Changing Color Theme
- React-native: Super expression must either be null or a function, not undefined
- How to store the data in React state after Querying it from GraphQL?
- Changing specific element in array in useState
- Add new objects to the nested array in react
- When I click on Tab more than once the back button works incorrectly
- React - Doesn't work refreshing page on react app
- Change color of range column chart in ApexCharts
- render hashmap data in react
- How to push or create a new array with options and labels in ReactJS?
- How to type any param with React Typescript in Functional Component
- How can we receive a csv file in server side react hooks web app
- How to write A React component library that allow scss variables to be overwriten
- React-router: ajax request onClick to Link
- How to pass values from Child Array to a Parent
- react-table not showing data
- Calling set function of useState causes infinite loop
- How to filter getByLabelText query when found multiple elements?
- Warning: Encountered two children with the same key, `[object Object]
- Testing with React's Jest and Enzyme when async componentDidMount
- How to use react-hook-form inside class Component?