RT# 77504 - Fixed error display issue, and javascript to prevent error.
[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   &>
11
12 The ID of the input field can be anything;  the ID of the DIV in which to display results
13 should be the input id plus '_result'.
14
15 </%doc>
16
17 <& '/elements/xmlhttp.html',
18     'url'  => $p.'misc/xmlhttp-validate_password.html',
19     'subs' => [ 'validate_password' ],
20     'method' => 'POST', # important not to put passwords in url
21 &>
22 <SCRIPT>
23 function add_password_validation (fieldid, submitid) {
24   var inputfield = document.getElementById(fieldid);
25   inputfield.onkeydown = function(e) {
26     var key;
27     if (window.event) { key = window.event.keyCode; }
28     else { key = e.which; }
29     if (key == '13') {
30       var check = checkPasswordValidation();
31       return check;
32     }
33   }
34   inputfield.onkeyup = function () {
35     var fieldid = this.id+'_result';
36     var resultfield = document.getElementById(fieldid);
37     if (this.value) {
38       resultfield.innerHTML = '<SPAN STYLE="color: blue;">Validating password...</SPAN>';
39       validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} %>','contactnum','<% $opt{'contactnum'} %>','password',this.value,
40         function (result) {
41           result = JSON.parse(result);
42           var resultfield = document.getElementById(result.fieldid);
43           if (resultfield) {
44             var errorimg = '<IMG SRC="<% $p %>images/error.png" style="width: 1em; display: inline-block; padding-right: .5em">';
45             var validimg = '<IMG SRC="<% $p %>images/tick.png" style="width: 1em; display: inline-block; padding-right: .5em">';
46             if (result.valid) {
47               resultfield.innerHTML = validimg+'<SPAN STYLE="color: green;">Password valid!</SPAN>';
48               if (submitid){ document.getElementById(submitid).disabled = false; }
49             } else if (result.error) {
50               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.error+'</SPAN>';
51               if (submitid){ document.getElementById(submitid).disabled = true; }
52             } else {
53               result.syserror = result.syserror || 'Server error';
54               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.syserror+'</SPAN>';
55               if (submitid){ document.getElementById(submitid).disabled = true; }
56             }
57           }
58         }
59       );
60     } else {
61       resultfield.innerHTML = '';
62     }
63   };
64 }
65
66 add_password_validation('<% $opt{'fieldid'} %>', '<% $opt{'submitid'} %>');
67 </SCRIPT>
68
69 <%init>
70 my %opt = @_;
71 </%init>
72
73