2 Replies Latest reply on Jul 10, 2009 6:00 AM by MarkWalsh

    need code for validating email address

    jwewing

      I need what I hope to be a simple script but I do not know javascript at all and need some help. I am trying to validate an email address form field. I simply need it to check if there is an @ and a dot. If it does not contain those characters then it alerts the user and puts focus back on the email form field. If the email address does contain the @ and dot then it will just allow tabbing to the next field.

       

      I made an attempt at just getting a message to see if it would recognize the @ but it doesnt work properly and dont know what I did wrong. No matter what was typed into the field I always get the message saying Email address is correct.

       

      var email = this.getfield("Text17");

      if(email = "@")

      {

      app.alert("Email address is Correct");

      }

      else

      {

      app.alert("Email address is not Correct");

      }

        • 1. Re: need code for validating email address
          try67 MVP & Adobe Community Professional

          To validate an email adress the best way is to use a Regular Expression, but anyway your problem is that you're using the assignment operator (=) instead of the comparison operator (==).

          • 2. Re: need code for validating email address
            MarkWalsh Level 4

            Here's a function that uses regular expressions to validate. I have 3 different regular expressions (some I probably found online) in this function that you can try.

             

             

            function validateEmail (emailAddress) {

            //var re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z] $/

             

            //var re = /^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-] +(\.[a-z0-9-]+)*\.([a-z]{2,})$/

             

            var re = /^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*) *(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x 01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d| [01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(an gle)>)$/

             

            if (emailAddress.length > 0) {

            if (re.test(emailAddress)) {

            //Field is valid

            return true

                } else {

            var alertText = 'You have entered an improper Email address.'

                app.beep()

            app.alert (alertText)

            return false

            }

            }

            }