forked from stripe-archive/jquery.payment
-
Notifications
You must be signed in to change notification settings - Fork 105
/
phone_number.coffee
93 lines (70 loc) · 2.61 KB
/
phone_number.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
$ = jQuery
hasTextSelected = $.formance.fn.hasTextSelected
reFormatPhoneNumber = (phoneNumberString) ->
phoneNumber = phoneNumberString.replace(/\D/g, '').match(/^(\d{0,3})?(\d{0,3})?(\d{0,4})?$/)
# return unless phoneNumber?
[phoneNumber, areaCode, first3, last4] = phoneNumber
text = ''
text += "(#{areaCode}" if areaCode?
text += ") " if areaCode?.length is 3
text += "#{first3}" if first3?
text += " - " if first3?.length is 3
text += "#{last4}" if last4?
return text
restrictPhoneNumber = (e) ->
$target = $(e.currentTarget)
digit = String.fromCharCode(e.which)
return unless /^\d+$/.test(digit)
return if hasTextSelected($target)
value = $target.val() + digit
value = value.replace(/\D/g, '')
return false if value.length > 10
formatPhoneNumber = (e) ->
digit = String.fromCharCode(e.which)
return unless /^\d+$/.test(digit)
$target = $(e.currentTarget)
val = $target.val() + digit
text = reFormatPhoneNumber(val)
e.preventDefault()
$target.val(text)
formatBackPhoneNumber = (e) ->
# If shift+backspace is pressed
return if e.meta
$target = $(e.currentTarget)
value = $target.val()
# Return unless backspacing
return unless e.which is 8
# Return if focus isn't at the end of the text
return if $target.prop('selectionStart')? and
$target.prop('selectionStart') isnt value.length
# Removes trailing spaces,brackets and dashes when
# hitting backspace at one of the jumps
if /\(\d$/.test(value)
e.preventDefault()
$target.val('')
else if /\d\)(\s)+$/.test(value)
e.preventDefault()
$target.val(value.replace(/\d\)(\s)*$/, ''))
else if /\d(\s|\-)+$/.test(value)
e.preventDefault()
$target.val(value.replace(/\d(\s|\-)+$/, ''))
formatPastePhoneNumber = (e) ->
setTimeout => # it takes a bit of time for the paste event to add the input, so wait a bit
$target = $(e.currentTarget)
val = $target.val()
text = reFormatPhoneNumber(val)
$target.val(text)
$.formance.fn.format_phone_number = ->
@.formance('restrictNumeric')
@on('keypress', restrictPhoneNumber)
@on('keypress', formatPhoneNumber)
@on('keydown', formatBackPhoneNumber)
@on('paste', formatPastePhoneNumber)
this
$.formance.fn.validate_phone_number = ->
val = $(this).val()
return false unless val?
val = val.replace(/\(|\)|\s+|-/g, '')
return false unless /^\d+$/.test(val)
# [areaCode, first3, last4] = val.match(/\d+/g)
return val.replace(/\D/g, '').length is 10 # replaces all non digits [^0-9] with ''