Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need to do validation in js(im terrible)

    <html>
    <head>
    <title>Exam entry</title>
    <script language="javascript" type="text/javascript">
    function validateForm() {
    var result = true;
    var msg="";
    if (document.ExamEntry.name.value=="") {
    msg+="You must enter your name \n";
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color="red";
    result = false;
    }
    if (document.ExamEntry.subject.value=="") {
    msg+="You must enter the subject \n";
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color="red";
    result = false;
    }
    if (document.ExamEntry.ExaminationNumber.value=="") {
    msg+="You must enter your examination number \n";
    document.ExamEntry.subject.focus();
    document.getElementById('ExaminationNumber').style.color="red";
    result = false;
    }
    else if (document.ExamEntry.ExaminationNumber.value.length 4)
    msg+="you muse enter 4 characters exactly \n";
    document.ExamEntry.subject.focus();
    document.getElementById('ExaminationNumber').style.color="red";
    result = false;
    }
    if(msg==""){
    return result;
    }
    {
    alert(msg)
    return result;
    }
    }


    I need to make the exam number validation 4 characters exactly and i dont know how please help?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Is this homework?

    Code:
    else if (document.ExamEntry.ExaminationNumber.value.length !=4)
    But 4 spaces or ???? will satisfy that test.

    You say 4 characters but I would have thought that a number consisted of digits. I answered this same question very recently at
    Require/Restrict Text Input

    Please note my other comments in that thread.

    “If liberty means anything at all, it means the right to tell people what they do not want to hear.” - George Orwell, English novelist and journalist, 1903-1950.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    if (document.ExamEntry.ExamNumber.value.length < 4 || document.ExamEntry.ExamNumber.value.length > 4 ) {
    		msg+="The exam number must be at 4 characters in length \n";
    		result = false;
    		}
    That should make it work Max

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Quote Originally Posted by Samarawrus View Post
    Code:
    if (document.ExamEntry.ExamNumber.value.length < 4 || document.ExamEntry.ExamNumber.value.length > 4 ) {
    		msg+="The exam number must be at 4 characters in length \n";
    		result = false;
    		}
    That should make it work Max
    <4 or >4 is the same as !=4.

    But as I say an Exam Number must surely be a number! And four spaces will satisfy your test.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •