Extract username from the content of the e-mail address

If, in a registration form, it is desired to extract the username from the e-mail address, in order to simplify data entry, the SPLIT function can be used. This function generating a table of values (username, domain), the first value ([0]) will be chosen, the e-mail address domain being obviously the value [1].

If you want the username to be displayed in real time (as letters are entered), change the ONCHANGE event to ONKEYUP. The username will appear only after the letter from the argument of the SPLIT function is entered; to prevent the expression “undefined” from appearing until the “@” symbol is entered, the code of that function can be adjusted a bit, as shown below.

If you want to display the username in an element other than input-text (for example, span), change the line username.value = id_email;  with username.innerHTML = id_email;

Source: StackOverflow.com (1), StackOverflow.com (2)

Author: Ovidiu.S

I am quite passionate about this professional area as if I know something - no matter how little - I want to share it with others.

Leave a Reply

Your email address will not be published.