cb1502939eb22dc4608160f5f066a3e64c185f68
[freeside.git] / httemplate / elements / validate_password.html
1 <%doc>
2
3 To validate passwords via javascript/xmlhttp:
4
5   <INPUT ID="password_field" TYPE="text">
6   <DIV ID="password_field_result">
7   <& '/elements/validate_password.html', 
8       fieldid  => 'password_field',
9       svcnum   => $svcnum,
10       pkgnum   => $pkgnum, # used if the service doesn't exist yet
11   &>
12
13 The ID of the input field can be anything;  the ID of the DIV in which to display results
14 should be the input id plus '_result'.
15
16 </%doc>
17
18 <& '/elements/xmlhttp.html',
19     'url'  => $p.'misc/xmlhttp-validate_password.html',
20     'subs' => [ 'validate_password' ],
21     'method' => 'POST', # important not to put passwords in url
22 &>
23 <SCRIPT>
24 function add_password_validation (fieldid) {
25   var inputfield = document.getElementById(fieldid);
26   inputfield.onchange = function () {
27     var fieldid = this.id+'_result';
28     var resultfield = document.getElementById(fieldid);
29     if (this.value) {
30       resultfield.innerHTML = '<SPAN STYLE="color: blue;">Validating password...</SPAN>';
31       validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} |js_string %>','pkgnum','<% $opt{'pkgnum'} |js_string %>','contactnum','<% $opt{'contactnum'} |js_string %>','password',this.value,
32         function (result) {
33           result = JSON.parse(result);
34           var resultfield = document.getElementById(result.fieldid);
35           if (resultfield) {
36             var errorimg = '<IMG SRC="<% $p %>images/error.png" style="width: 1em; display: inline-block; padding-right: .5em">';
37             var validimg = '<IMG SRC="<% $p %>images/tick.png" style="width: 1em; display: inline-block; padding-right: .5em">';
38             if (result.valid) {
39               resultfield.innerHTML = validimg+'<SPAN STYLE="color: green;">Password valid!</SPAN>';
40             } else if (result.error) {
41               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.error+'</SPAN>';
42             } else {
43               result.syserror = result.syserror || 'Server error';
44               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.syserror+'</SPAN>';
45             }
46           }
47         }
48       );
49     } else {
50       resultfield.innerHTML = '';
51     }
52   };
53 }
54 add_password_validation('<% $opt{'fieldid'} %>');
55 </SCRIPT>
56
57 <%init>
58 my %opt = @_;
59 </%init>
60
61