Add paste event check for submit button validation
[arvados.git] / apps / workbench / app / views / users / _setup_popup.html.erb
1 <div class="modal-dialog">
2   <div class="modal-content">
3
4     <%= form_tag setup_user_path, {id: 'setup_form', name: 'setup_form', method: 'get',
5         class: 'form-search', remote: true} do %>
6
7     <div class="modal-header">
8       <button type="button" class="close" onClick="reset_form()" data-dismiss="modal" aria-hidden="true">&times;</button>
9       <h4 class="modal-title">Setup User</h4>
10     </div>
11
12     <div class="modal-body">
13       <% if @object%>
14         <% uuid = @object.uuid %>
15         <% email = @object.email %>
16       <% end %>
17       <% disable_email = uuid != nil %>
18       <% identity_url_prefix = @current_selections[:identity_url_prefix] %>
19       <% disable_url_prefix = identity_url_prefix != nil %>
20       <% selected_repo = @current_selections[:repo_name] %>
21       <% selected_vm = @current_selections[:vm_uuid] %>
22
23       <input id="user_uuid" maxlength="250" name="user_uuid" type="hidden" value="<%=uuid%>">
24       <div class="form-group">
25         <label for="email">Email</label>
26         <% if disable_email %>
27         <input class="form-control" id="email" maxlength="250" name="email" type="text" value="<%=email%>" disabled>
28         <% else %>
29         <input class="form-control" id="email" maxlength="250" name="email" type="text">
30         <% end %>
31       </div>
32       <div class="form-group">
33         <label for="openid_prefix">Identity URL Prefix</label>
34         <% if disable_url_prefix %>
35         <input class="form-control" id="openid_prefix" maxlength="250" name="openid_prefix" type="text"
36                value="<%=identity_url_prefix%>" disabled=true>
37         <% else %>
38         <input class="form-control" id="openid_prefix" maxlength="250" name="openid_prefix" type="text"
39                value="<%= Rails.configuration.default_openid_prefix %>">
40         <% end %>
41       </div>
42       <div class="form-group">
43         <label for="repo_name">Repository Name</label>
44         <input class="form-control" id="repo_name" maxlength="250" name="repo_name" type="text" value="<%=selected_repo%>">
45       </div>
46       <div class="form-group">
47         <label for="vm_uuid">Virtual Machine</label>
48         <select class="form-control" name="vm_uuid">
49           <option value="" <%= 'selected' unless selected_vm %>>
50             Choose One:
51           </option>
52           <% @vms.each do |vm| %>
53             <option value="<%=vm.uuid%>"
54                     <%= 'selected' if selected_vm == vm.uuid %>>
55               <%= vm.hostname %>
56             </option>
57           <% end %>
58         </select>
59       </div>
60     </div>
61
62     <div class="modal-footer">
63       <button type="submit" id="register" class="btn btn-primary" autofocus>Submit</button>
64       <button class="btn btn-default" onClick="reset_form()" data-dismiss="modal" aria-hidden="true">Cancel</button>
65     </div>
66
67     <% end #form %>
68   </div>
69 </div>
70
71 <script>
72   var $input = $('input:text'),
73   $register = $('#register');
74
75   var email_disabled = document.forms["setup_form"]["email"].disabled;
76   var email_value = document.forms["setup_form"]["email"].value;
77   var prefix_value = document.forms["setup_form"]["openid_prefix"].value;
78   if ((email_disabled == false) && (email_value == null || email_value == "" ||
79         prefix_value == null || prefix_value == "")) {
80     $register.attr('disabled', true);
81   }
82
83   $input.on('keyup paste mouseleave', function() {
84     var trigger = false;
85
86     var email_disabled = document.forms["setup_form"]["email"].disabled;
87     var email_value = document.forms["setup_form"]["email"].value;
88     var prefix_value = document.forms["setup_form"]["openid_prefix"].value;
89
90     var emailRegExp = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
91     var validEmail = false;
92     if (emailRegExp.test(email_value )) {
93       validEmail = true;
94     }
95
96     if ((email_disabled == false) && (!validEmail || email_value == null ||
97             email_value == "" || prefix_value == null || prefix_value == "")){
98       trigger = true;
99     }
100
101     trigger ? $register.attr('disabled', true) : $register.removeAttr('disabled');
102   });
103
104   function reset_form() {
105     $('#email').val("");
106     $('#openid_prefix').val("");
107     $('#repo_name').val("");
108     $('select').val('')
109   }
110
111 </script>