PDF Form - Validate e-mail address field via JavaScript  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

smaziarz
User
Posts: 18
Joined: Fri Aug 13, 2021 3:35 pm

PDF Form - Validate e-mail address field via JavaScript

Post by smaziarz »

I have several e-mail fields and would like to validate them. I see the ability for Custom Action | Run a JavaScript. However, I haven't been able to find any examples or documentation about doing so. A few questions:

  1. How do I even reference the value of the current field?
  2. I presume you'd return a 1 if it's valid or 0 if not?
  3. Do you get to supply the error message displayed if invalid? If so, how?

Thanks!
smaziarz
User
Posts: 18
Joined: Fri Aug 13, 2021 3:35 pm

Re: PDF Form - Validate e-mail address field via JavaScript  SOLVED

Post by smaziarz »

Claude came through once again!!

Code: Select all

var v = (event.value == null) ? "" : String(event.value).replace(/^\s+|\s+$/g, "");

if (v === "") {
    event.rc = true;   // empty is OK
} else {
    var re = /^[A-Za-z0-9._%+'-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$/;
    event.rc = re.test(v);
    if (!event.rc) app.alert("Please enter a valid e-mail address.");
}
User avatar
Sean - PDF-XChange
Site Admin
Posts: 1469
Joined: Wed Sep 14, 2016 5:42 pm

Re: PDF Form - Validate e-mail address field via JavaScript

Post by Sean - PDF-XChange »

:)
Sean Godley
Technical Writer
PDF-XChange Co LTD
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623