Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hypha
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ots
hypha
Commits
fffcecec
Commit
fffcecec
authored
6 years ago
by
Fredrik Jonsson
Browse files
Options
Downloads
Patches
Plain Diff
Use Mailgun via javascript to validate e-mail addresses in submission forms.
parent
4e1a4fd5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
opentech/apply/funds/templates/funds/application_base.html
+4
-0
4 additions, 0 deletions
opentech/apply/funds/templates/funds/application_base.html
opentech/static_src/src/javascript/apply/mailgun-validator.js
+122
-0
122 additions, 0 deletions
...tech/static_src/src/javascript/apply/mailgun-validator.js
with
126 additions
and
0 deletions
opentech/apply/funds/templates/funds/application_base.html
+
4
−
0
View file @
fffcecec
...
...
@@ -42,3 +42,7 @@
{% endif %}
</div>
{% endblock %}
{% block extra_js %}
<script
src=
"{% static 'js/apply/mailgun-validator.js' %}"
></script>
{% endblock %}
This diff is collapsed.
Click to expand it.
opentech/static_src/src/javascript/apply/mailgun-validator.js
0 → 100644
+
122
−
0
View file @
fffcecec
(
function
(
$
)
{
'
use strict
'
;
$
.
fn
.
mailgunValidator
=
function
(
options
)
{
return
this
.
each
(
function
()
{
var
thisElement
=
$
(
this
);
thisElement
.
focusout
(
function
(
e
)
{
// Trim string and autocorrect whitespace issues
var
elementValue
=
thisElement
.
val
();
elementValue
=
$
.
trim
(
elementValue
);
thisElement
.
val
(
elementValue
);
// Attach event to options
options
.
e
=
e
;
run_validator
(
elementValue
,
options
,
thisElement
);
});
});
};
function
run_validator
(
address_text
,
options
,
element
)
{
// Abort existing AJAX Request to prevent flooding
if
(
element
.
mailgunRequest
)
{
element
.
mailgunRequest
.
abort
();
element
.
mailgunRequest
=
null
;
}
// don't run validator without input
if
(
!
address_text
)
{
return
;
}
// validator is in progress
if
(
options
&&
options
.
in_progress
)
{
options
.
in_progress
(
options
.
e
);
}
// don't run dupicate calls
if
(
element
.
mailgunLastSuccessReturn
)
{
if
(
address_text
===
element
.
mailgunLastSuccessReturn
.
address
)
{
if
(
options
&&
options
.
success
)
{
options
.
success
(
element
.
mailgunLastSuccessReturn
,
options
.
e
);
}
return
;
}
}
// length and @ syntax check
var
success
=
false
;
var
error_message
=
false
;
if
(
address_text
.
length
>
512
)
{
error_message
=
'
Email address exceeds maxiumum allowable length of 512.
'
;
}
else
if
(
address_text
.
split
(
'
@
'
).
length
-
1
!==
1
)
{
error_message
=
'
Email address must contain only one @.
'
;
}
if
(
error_message
)
{
if
(
options
&&
options
.
error
)
{
options
.
error
(
error_message
,
options
.
e
);
}
return
;
}
// timeout incase of some kind of internal server error
var
timeoutID
=
setTimeout
(
function
()
{
error_message
=
'
Error occurred, unable to validate address.
'
;
if
(
!
success
)
{
// Abort existing AJAX Request for a true timeout
if
(
element
.
mailgunRequest
)
{
element
.
mailgunRequest
.
abort
();
element
.
mailgunRequest
=
null
;
}
if
(
options
&&
options
.
error
)
{
options
.
error
(
error_message
,
options
.
e
);
}
}
},
30000
);
// 30 seconds
// make ajax call to get validation results
element
.
mailgunRequest
=
$
.
ajax
({
type
:
'
GET
'
,
url
:
'
https://api.mailgun.net/v3/address/validate
'
,
data
:
{
address
:
address_text
,
api_key
:
options
.
api_key
},
dataType
:
'
jsonp
'
,
crossDomain
:
true
,
success
:
function
(
data
,
status_text
)
{
clearTimeout
(
timeoutID
);
element
.
mailgunLastSuccessReturn
=
data
;
if
(
options
&&
options
.
success
)
{
options
.
success
(
data
,
options
.
e
);
}
},
error
:
function
(
request
,
status_text
,
error
)
{
clearTimeout
(
timeoutID
);
error_message
=
'
Error occurred, unable to validate address.
'
;
if
(
options
&&
options
.
error
)
{
options
.
error
(
error_message
,
options
.
e
);
}
}
});
}
$
(
'
input[type=email]
'
).
each
(
function
()
{
$
(
this
).
after
(
'
<div class="status"></div>
'
).
mailgunValidator
({
api_key
:
'
pubkey-b3ea454f53bcd621d767e64f0b4ef0ac
'
,
error
:
validation_error
});
});
function
validation_error
(
error_message
)
{
$
(
'
.status
'
).
html
(
error_message
);
}
})(
jQuery
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment