Copy link to clipboard
Copied
Hi,
I am creating a Contact Form with Adobe. One of the required fields is an email address.
I want the field to error out if they do not complete certain things within this. For example, I cannot have any 'spaces' after they have finished typing and I need to ensure the @ symbol is used. This will also need to be a 'required' field.
Is anyone able to assist? I have read through multiple forms and support articles and cannot find what I need.
Thanks!
Copy link to clipboard
Copied
This would require a validation script.
For example, you can use something like this:
if (event.value) {
if (/ /.test(event.value)==true) {app.alert("You may not enter a space.");
event.rc = false;
} else if (/@/.test(event.value)==false) {
app.alert("You must enter the \"@\" symbol.");
event.rc = false;
}
}
Copy link to clipboard
Copied
Have you looked at using the RegExp object to validate the format of the email address?
One such custom validation script could be:
if(/^\S+@\S+$/.test(event.value) == false)
{
app.alert("Invalid email address!", 1, 0, "Invalid Email Address");
event.rc = false;
}
This will test for leading, embedded, and trailing scripts along with the "@" between the two stings. There are other RegExps that include a more extensive testing of the user name and domain names.
Validation does not "error" out the form but alerts the user to an error. One can lock or prevent the submission of the form by email by adding conditional execution of code based on the validation, this could include hiding the submit button or just not executing the submit command(s).
"error" is a vary specific item within JavaScript.
Copy link to clipboard
Copied
Acrobat/Reader have a built-in routine to validate email addresses that's pretty good. Because it's not available in other JavaScript PDF viewers, you can make it a custom function in your forms and use it in a custom Validate script. Here's the code:
function eMailValidate(emailStr) {
var emailPat = /^(.+)@(.+)$/;
var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars = "[^\\s" + specialChars + "]";
var quotedUser = "(\"[^\"]*\")";
var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom = validChars + "+";
var word = "(" + atom + "|" + quotedUser + ")";
var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
var matchArray = emailStr.match(emailPat);
if (matchArray == null) {
return false;
}
var user = matchArray[1];
var domain = matchArray[2];
if (user.match(userPat) == null) {
return false;
}
var IPArray = domain.match(ipDomainPat);
if (IPArray != null) {
for (var i = 1; i <= 4; i++) {
if (IPArray > 255) {
return false;
}
}
return true;
}
var domainArray = domain.match(domainPat);
if (domainArray == null) {
return false;
}
var atomPat = new RegExp(atom, "g");
var domArr = domain.match(atomPat);
var len = domArr.length;
if (domArr[domArr.length - 1].length < 2) {
return false;
}
if (len < 2) {
return false;
}
return true;
}
I'd suggest renaming it this function to something like "eMailValidate2" and placing it in a document-level JavaScript. You can then use it in a field's validate script like this:
if (event.value) {
if (!eMailValidate2(event.value)) {
// You don't have to include this since it will restore the previous value (perhaps annoying the user),
// but it will prevent an invalid entry
event.rc = false;
// Give some feedback to the user
app.alert("Please enter a valid email address.", 3);
}
}