Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update Text only once after clicking button

Currently my project looks like this:
https://jsfiddle.net/AllmightyChaos/w9jm4bgr/9/

After you’d click on one of the buttons, it should fill or replace the text, so its a valid E-Mail Adress.

For example, if you have test123 and click on the gmail-button, it should insert @gmail.com after it.
But if you have test123@outlook.com, it should remove the "@outlook.com" and replace it with the text on the button. (clicking on gmail-button = test123@gmail.com and so on…)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Short form:
If you click onto the button, remove all after the "@" and replace it with the text that is in the button.

Thanks! 🙂

>Solution :

Using regular expressions you can easily remove the suffixes. Try this:

function cleanText(value){
    return value.replace(/(@.+.com)$/, '')
}

$(function () {
    $('#gmail-button').on('click', function () {
        var text = $('#text');
        text.val(cleanText(text.val()) + '@gmail.com');    
    });
});

$(function () {
    $('#outlook-button').on('click', function () {
        var text = $('#text');
        text.val(cleanText(text.val()) + '@outlook.com');    
    });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading