views:

44

answers:

2

Hi This is a simple question i guess: My requirement is in the Text box whenever user enters mail, the domain name should be appended automatically.Also the name entered should not allow special characters except '.' '_' in reality. Also the user should not be allowed to add any other domain except which is specified in the property file.This is in JAVASCRIPT

+2  A: 

Why not simply show

|_________| @yourdomain.com

and do the appending either when submitting (onsubmit event) or on the server side?

e.g.

function email_form_submit(event)
{
    event = event || window.event;

    if (event.preventDefault)
        event.preventDefault();

    if (event.stopPropagation)
        event.stopPropagation();
    else
        event.cancelBubble = true;

    var form = document.getElementByID('your form id');
    var element = document.getElementByID('your email form element id');
    element.value = element.value + "yourdomain.com";

    form.submit();

    return false;
}

See http://msdn.microsoft.com/en-us/library/ms536972%28VS.85%29.aspx

EDIT
quick validation regex. Note: not tested

function validate_email_address(event)
{
    event = event || window.event;

    var regex = /^[a-zA-Z0-9._]+@yourdomain\.com$/;

    var element = document.getElementByID('your email form element id');
    element.value = element.value + "yourdomain.com";

    if (!regex.test(element.value))
    {
        alert("Invalid Email");
        if (event.preventDefault)
            event.preventDefault();
        return false;
    }

}

Edit: fixed syntax error

Jonathan Fingland
Good way to do this. +1
rahul
actully I am expecting a regular expression kind
GustlyWind
there are many regular expressions for email and most are incorrect. Since you seem to have specific restrictions on the email, I'll add a quick validation function with regex, but Soviut's suggestion of going with a validation library is pprobably where you want to go
Jonathan Fingland
A: 

I highly recommend using an existing javascript form validation library to handle legal characters in a field. JQuery has several validation plugins, and a quick google search for "javascript validation libraries" yielded this library.

However, for domains, I would simply not allow ANY domain name in your field and append the domain name on the server side. This would make things much easier because to make sure they don't include a domain name in the field, you simply make the @ symbol one of the illegal characters in your field validation rules.

Soviut
I m sorry I dont have the choice to use JQUERY but I understand it has lots of features
GustlyWind