Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const smartInput = (event, fieldIndex, fields)=> {
- const controlKeys = [
- "Tab",
- "Delete",
- "Backspace",
- "ArrowRight",
- "ArrowLeft",
- "Shift",
- "ArrowUp",
- "ArrowDown"
- ]
- const isSpecialKey = controlKeys.includes(event.key)
- const isValidForDigit = /^\d$/.test(event.key)
- const isValidForName = /[a-zA-Z]|\s/.test(event.key)
- const isValidForDate = /\d|\//.test(event.key)
- //check if entered key is part of allowed keys(control characters), numbers,
- //dates
- const canProceed = isSpecialKey || isValidForDigit || isValidForName ||
- isValidForDate
- if (canProceed) {
- //check if we are in the credit card digits input zone
- if (fieldIndex <= 3 && isValidForDigit) {
- if (appState.cardDigits[fieldIndex] === undefined) {
- appState.cardDigits[fieldIndex] = []
- }
- //add key to card digits
- appState.cardDigits[fieldIndex].push(event.key)
- let target = event.target
- let {value, selectionStart} = target
- //get up to our current cursor position
- target.value = value.substr(0, selectionStart) + event.key +
- value.substr(selectionStart + 1)
- setTimeout(()=> {
- // call detectcard if it is our first round of entering digits
- if (fieldIndex === 0 && target.value.length >= 4) {
- const first4Digits = appState.cardDigits[0]
- console.log(first4Digits)
- detectCardType(first4Digits)
- }
- smartCursor(event, fieldIndex, fields)
- target.value = target.value.replace(/\d/g, '#')
- }, 500)
- }
- else {
- setTimeout(()=> {
- smartCursor(event, fieldIndex, fields)
- }, 500)
- }
- }
- else {
- event.preventDefault()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement