8c8cf42799e93b50d2352486b9660879ed60a04a
[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, submitid) {
25   var inputfield = document.getElementById(fieldid);
26   inputfield.onkeydown = function(e) {
27     var key;
28     if (window.event) { key = window.event.keyCode; }
29     else { key = e.which; }
30     if (key == '13') {
31       var check = checkPasswordValidation();
32       return check;
33     }
34   }
35   inputfield.onkeyup = function () {
36     var fieldid = this.id+'_result';
37     var resultfield = document.getElementById(fieldid);
38     if (this.value) {
39       resultfield.innerHTML = '<SPAN STYLE="color: blue;">Validating password...</SPAN>';
40       validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} %>','pkgnum','<% $opt{'pkgnum'} %>','contactnum','<% $opt{'contactnum'} %>','password',this.value,
41         function (result) {
42           result = JSON.parse(result);
43           var resultfield = document.getElementById(result.fieldid);
44           if (resultfield) {
45             var errorimg = '<IMG SRC="<% $p %>images/error.png" style="width: 1em; display: inline-block; padding-right: .5em">';
46             var validimg = '<IMG SRC="<% $p %>images/tick.png" style="width: 1em; display: inline-block; padding-right: .5em">';
47             if (result.valid) {
48               resultfield.innerHTML = validimg+'<SPAN STYLE="color: green;">Password valid!</SPAN>';
49               if (submitid){ document.getElementById(submitid).disabled = false; }
50             } else if (result.error) {
51               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.error+'</SPAN>';
52               if (submitid){ document.getElementById(submitid).disabled = true; }
53             } else {
54               result.syserror = result.syserror || 'Server error';
55               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.syserror+'</SPAN>';
56               if (submitid){ document.getElementById(submitid).disabled = true; }
57             }
58           }
59         }
60       );
61     } else {
62       resultfield.innerHTML = '';
63     }
64   };
65 }
66
67 add_password_validation('<% $opt{'fieldid'} %>', '<% $opt{'submitid'} %>');
68 </SCRIPT>
69
70 <%init>
71 my %opt = @_;
72 </%init>
73
74