0

hopefully that makes sense.... basically I have a form and want to take the input for the "email" field and truncate/trim the value before the "@" symbol and give that value to another hidden field.

EDIT/ADDED: Also, if possible in the same function, it would be great to automatically generate and append a random number (between 01 and 99 would suffice) to ensure there are no "duplicates/matching "hiddenfield" values... for example if the email address is something nondescript like info@blahblah.com, where "info" might be blocked if already in system but info46 and info07 would be fine.

IF it is not appropriate to add to the question like this I apologize and will edit it out, mark the question as answered based on original criteria and open a new question that hopefully can add to this.

So basically if someone entered "JohnSmith@hotmail.com", "JohnSmith26" (as an example) would be assigned as the value for another hidden form field.

example:

<label>Email Address:</label><input type="text" id="email" name="email" value=""     size="30" maxlength="80"/>

    <input type="hidden" id="hiddenfield" name="hiddenfield" value="truncated email" />

I saw this thread but am not using coldfusion etc and am hoping to just find a simple way to do similar within the form: how do I trim an email input address so only the data before the @ is inputted into the database?

am looking for the best way using javascript/jQuery to assign the truncated value to the other field.

Community
  • 1
  • 1
Denny
  • 15
  • 6
  • Check here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split – elclanrs Jun 17 '13 at 21:58

2 Answers2

0

First split the email address, then use regex to remove white spaces and then assign it to the hidden field.

var email = document.getElementById('email').value.split('@')[0],
    hidden = document.getElementById('hiddenfield'),
    trimmedValue = email.replace(/\s+/g, '');

hidden.value = trimmedValue ;
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • An e–mail address shouldn't have any white space, so you can use something like: `/([^\s@]+)@/` to get all non–whitespace characters before the @, e.g. `(emailAddress.match(/([^\s@]+)@/) || [])[1] || '';` – RobG Jun 17 '13 at 22:17
0

You probably want something like this

Javascript

var email = document.getElementById("email"),
    hidden = document.getElementById("hiddenfield");

function transferTruncated() {
    var target = this,
        name;

    if (target.value.indexOf("@") !== -1) {
        name = target.value.split("@")[0].trim();

        if (name && name.search(/\s/) === -1) {
            hidden.value = name;
        } else {
            hidden.value = "truncated email";
        }
    } else {
        hidden.value = "truncated email";
    }
}

email.addEventListener("change", transferTruncated, false);

On jsfiddle

Note: I removed the hidden type from the input field so that you can see it, and you may want to check that the name is further valid too before assigning it.

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • wow that was fast AND spot on! I unfortunately forgot to include another aspect which I have edited/added above now. But, if that is not acceptable to edit questions once worked on/answered I will remove it and mark this as solved based on your solution. Hoping that it can be added to this function though. – Denny Jun 18 '13 at 14:22
  • It can be easily done, just have another function that creates the random number and do this `hidden.value = name + randomNumber();`. If you don't know how to generate such a number it would probably be best to post a separate question (or just search SO I'm sure there are plenty of examples), based on the title of this current question. – Xotic750 Jun 18 '13 at 14:31