Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. const smartInput = (event, fieldIndex, fields)=> {
  2. const controlKeys = [
  3. "Tab",
  4. "Delete",
  5. "Backspace",
  6. "ArrowRight",
  7. "ArrowLeft",
  8. "Shift",
  9. "ArrowUp",
  10. "ArrowDown"
  11. ]
  12. const isSpecialKey = controlKeys.includes(event.key)
  13. const isValidForDigit = /^\d$/.test(event.key)
  14. const isValidForName = /[a-zA-Z]|\s/.test(event.key)
  15. const isValidForDate = /\d|\//.test(event.key)
  16.  
  17. //check if entered key is part of allowed keys(control characters), numbers,
  18. //dates
  19. const canProceed = isSpecialKey || isValidForDigit || isValidForName ||
  20. isValidForDate
  21.  
  22. if (canProceed) {
  23.  
  24. //check if we are in the credit card digits input zone
  25. if (fieldIndex <= 3 && isValidForDigit) {
  26. if (appState.cardDigits[fieldIndex] === undefined) {
  27. appState.cardDigits[fieldIndex] = []
  28. }
  29.  
  30. //add key to card digits
  31. appState.cardDigits[fieldIndex].push(event.key)
  32.  
  33. let target = event.target
  34. let {value, selectionStart} = target
  35.  
  36. //get up to our current cursor position
  37. target.value = value.substr(0, selectionStart) + event.key +
  38. value.substr(selectionStart + 1)
  39.  
  40. setTimeout(()=> {
  41.  
  42. // call detectcard if it is our first round of entering digits
  43. if (fieldIndex === 0 && target.value.length >= 4) {
  44. const first4Digits = appState.cardDigits[0]
  45. console.log(first4Digits)
  46. detectCardType(first4Digits)
  47. }
  48.  
  49. smartCursor(event, fieldIndex, fields)
  50.  
  51. target.value = target.value.replace(/\d/g, '#')
  52. }, 500)
  53. }
  54. else {
  55. setTimeout(()=> {
  56. smartCursor(event, fieldIndex, fields)
  57. }, 500)
  58. }
  59. }
  60. else {
  61. event.preventDefault()
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement