Email format check in text field

Forums General discussion Email format check in text field

Tagged: ,

Viewing 4 reply threads
  • Author
    Posts
    • #30647
      Maciek Grischke
      Participant

      Hi all,

      I have a free-type text field for users to enter an email address, but as you know, people get it wrong. The text field allows users to enter multiple email addresses, but they are required to separate them with a semicolon. They get this wrong too.

      I need to be able to correct the format for users if they get it wrong, for example:

      “name @comany.com” – space in the email address (remove space)
      “name@comany.com other@comany.com” – no semicolon separating email addresses (place a semicolon separating all email addresses)
      “name @comany.com other@comany.com” – both, space in the email and no semicolon

      Is there a way to correct this for users as they type or maybe when a submit button is pressed?

      Many thanks

    • #30649
      Alexander Bautz
      Keymaster

      You can try adding something like this to your Custom JS:

      jQuery("#dffs_YOUR_MULTILINE_FIELD_INTERNAL_NAME textarea").on("blur", function(){
          var val = jQuery(this).val();
          if(val.split(" ").length > 1){
              spjs.dffs.alert({
                  "title": "Error in email address",
                  "msg": "You have one or more spaces in your email addresses - please correct"
              });
          }
      }).on("keyup", function(){
          var val = jQuery(this).val();
          if(val.split(" ").length > 0){
              jQuery(this).val(val.split(" ").join(";"));
          }
      });

      Replace YOUR_MULTILINE_FIELD_INTERNAL_NAME with your field internal name.

      Alexander

    • #30651
      Maciek Grischke
      Participant

      Thanks Alexander,

      The text field is a Single line of text, but I guess I could convert it into multiline.

      I was using this so far:

      //remove spaces from email addresses
      jQuery(“#dffs_Title input”).on(“keyup”,function(e){
      var str = jQuery(this).val();
      jQuery(this).val(str.replace(‘ ‘, ” ));
      });

      It will not allow users to enter any spaces but when it comes to separating email addresses, users will likely leave two email addresses merged together when they decide to use a space and not semicolon, and without looking.

      I know this one could be very tricky.

    • #30653
      Alexander Bautz
      Keymaster

      In my code example you can just change textarea to input.

      The snippet I provided above will trim spaces as you write, and also alert in case you paste content (right click > paste) with space in the field.

      Alexander

    • #30719
      Maciek Grischke
      Participant

      Thanks Alexander, it work’s great, however no alert is displayed for some reason.
      I tried with several spaces and different combinations. All spaces are replaced with semicolon, which is fine, but no alert 🙁

      • #30721
        Alexander Bautz
        Keymaster

        In this example it only alerts if you PASTE the contents using right click > paste. If you use Ctrl + v the keyup functionality will remove the space immediately.

        Alexander

Viewing 4 reply threads
  • You must be logged in to reply to this topic.