Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript Validation!

Options
  • 20-10-2004 7:46pm
    #1
    Closed Accounts Posts: 414 ✭✭


    Hi im just wondering if anybody can tell me how to validate an email address inside a html form. I want it to check for an '@' symbol and a '.' symbol. Here's what i have so far,


    [javascript]
    if(document.frmInput.email.value == " ")
    {
    alert("Please enter your email address.")
    document.frmInput.email.focus();
    return false;
    }

    [/javascript]


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Google for "Javascript email address validation regular expression" (without the quotes. I'm sure it'll bring up plenty of topics....


  • Registered Users Posts: 2,286 ✭✭✭SprostonGreen


    I'm only a beginner, so I'm not sure if this helps it might??


  • Registered Users Posts: 44 andersde10


    var email = document.frmInput.email.value
    if (email.indexOf('@') < 0) {
    alert ('not valid');
    }

    if (email.indexOf('.') < 0) {
    alert ('not valid');
    }


  • Closed Accounts Posts: 414 ✭✭Divine


    Ok i have tried some of that and this is what i have at the moment, however something is wrong because my form wont function properly since i have added the email validation script. CAn anyone help?

    Sorry about not using the tags but i dont know how but im going to try this!

    [php]
    <script>
    function checkInput ()
    {
    if(document.frmInput.username.value == "")
    {
    alert("Please enter Your Name.")
    document.frmInput.username.focus();
    return false;
    }
    if(document.frmInput.student_number.value == "")
    {
    alert("Please enter your Student Number.")
    document.frmInput.student_number.focus();
    return false;
    }
    if(isNaN(document.frmInput.student_number.value))
    {
    alert("Please enter in number.")
    document.frmInput.student_number.focus();
    return false;
    }

    if(document.frmInput.year.value == "")
    {
    alert("What year student are you?.")
    document.frmInput.year.focus();
    return false;
    }
    if(document.frmInput.course.value == "")
    {
    alert("Please enter your course name.")
    document.frmInput.course.focus();
    return false;
    }
    if(document.frmInput.module.value == "")
    {
    alert("Please enter the module name");
    document.frmInput.module.focus();
    return false;
    }
    if(document.frmInput.lecturer.value == "")
    {
    alert("What is your lecturers name?")
    document.frmInput.lecturer.focus();
    return false;
    }

    if(document.frmInput.assignment.value == "")
    {
    alert("Please enter the assignment name.")
    document.frmInput.assignment.focus();
    return false;
    }
    if(document.frmInput.result.value == "")
    {
    alert("Please enter the result of the assignment.")
    document.frmInput.result.focus();
    return false;
    }

    return true;
    }

    function checkEmail(frmInput) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frmInput.email.value)){
    return (true)
    }
    alert("Invalid E-mail Address! Please re-enter.")
    return (false)

    }
    return true;
    }
    </script>

    [/php]


  • Moderators, Politics Moderators Posts: 39,933 Mod ✭✭✭✭Seth Brundle


    I would get it to generate one message box containing all errors rather than having loads pop-up (say when the user pessed enter accidentally


  • Advertisement
  • Registered Users Posts: 44 andersde10


    kbannon wrote:
    I would get it to generate one message box containing all errors rather than having loads pop-up (say when the user pessed enter accidentally

    correct. but an even better solution would be to do all the validation server side via say php and then you wouldn't even need javascript. Server side gives you the huge advantage of being able to flag the errors beside the appropriate input fields which makes it alot easier to spot for the user, as well as being completely accessible for those who don't have javascript.


  • Moderators, Politics Moderators Posts: 39,933 Mod ✭✭✭✭Seth Brundle


    You are right andersde10, but I was made an assumption* that the OP wasn't familiar with server side scripting.

    * I know that assumption is the mother of all fupp ups!


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    andersde10 wrote:
    correct. but an even better solution would be to do all the validation server side via say php and then you wouldn't even need javascript. Server side gives you the huge advantage of being able to flag the errors beside the appropriate input fields which makes it alot easier to spot for the user

    Tsk. Don't be silly, JS can do that too.
    , as well as being completely accessible for those who don't have javascript.

    Correct.


  • Registered Users Posts: 44 andersde10


    hostyle wrote:
    Tsk. Don't be silly, JS can do that too.



    Correct.

    sure it can, but its way easier and much better practice doing that sort of thing server side. specially if you gotta start thinking about all the different browsers.


Advertisement