-1

I have written the following code in html and the javascript file is also embedded below

<!DOCTYPE html>
<meta charset="utf-8">
<HTML>
<head>
<link rel="stylesheet" href="admin.css"/>
<script type="text/javascript" src="common.js"></script>
</head>
<BODY>
<div class="adminnav" id="adminnav">
<ul>
<li><a href="#">Insert</a>
<ul>
<li><a href="addcourse.html">Insert Course</a></li>
<li><a href="addstudent.html">Insert Student</a></li>
<li><a href="addteacher.html">Insert Teacher</a></li>
<li><a href="addsubject.html">Insert Subject</a></li>
</ul>
</li>
</ul>
</div>
<div class="addcourse" >
<form name="course" onSubmit='return validcourse()' method="POST">
Course Name:
  <input type="text" name="coursename" id="cname" /><br>
Duration:<input type="text" name="cd"/>
<br>
Course Id:<input name="cid" type="text" /><br>
<input  type="submit" value="submit" name="submit"><input type="reset" name="coursereset">
</form>
</div>
</BODY>
</HTML>

common.js

function validcourse()
{
    var course_name=document.course.coursename;
    var course_duration=document.course.cd;
    var course_id=document.course.cid;

    if(course_name_valid(course_name))
    {
        {
            if(course_duration_valid(course_duration))
            {
                if(course_id_valid(cid))
                {
                }
            }
        }
    }


}

function course_name_valid(course_name)
{
    var letters=/^[A-Za-z]+$/;
    if(course_name.value.match(letters))
    {
        return true;
    }
    else
    {
        alert("Course name must have alphabets only");
        course_name.focus();
        return false;

    }
}

function course_duration_valid(course_duration)
{
    var letters=/^[1-9]+$/;
    if(cd.value.match(letters))
    {
        return true;
    }
    else
    {   
        alert("Course Duration can have numbers only");
        course_duration.focus();    
        retun false;
    }   


}

function course_id_valid(course_id)
{
    var letters=/^[0-9a-zA-z]+$/;
    if(course_id.value.match(letters))
    {
        return true;
    }
    else
    {
        alert('Course ID must have character and numeric values only');
        course_id.focus();
        return false;           
    }
}

My problem is that nothing is happening no error messages are given. Error messages are given only for course name but not for course duration and course id?

Here is a link to jsfiddle http://jsfiddle.net/amolkarale/aTfq6/1/

7
  • 2
    Put an alert at the top of your js and see if that executes. I suspect you are calling the wrong file. Commented Mar 13, 2013 at 23:02
  • I am calling common.js file which contains the code for validation.You can check the script tag. Commented Mar 13, 2013 at 23:05
  • Can you create a jsfiddle and update to post the link - much easier to debug that way Commented Mar 13, 2013 at 23:09
  • ok in a while i will do it Commented Mar 13, 2013 at 23:11
  • ok here is a link to fiddle jsfiddle.net/amolkarale/aTfq6/1 Commented Mar 13, 2013 at 23:26

3 Answers 3

1

You are not returning anything from validcourse. Add a return true; and return false; in there, to either let the submission continue or stop the submission from happening:

function validcourse()
{
    var course_name=document.course.coursename;
    var course_duration=document.course.cd;
    var course_id=document.course.cid;

    if(course_name_valid(course_name))
    {
        {
            if(course_duration_valid(course_duration))
            {
                if(course_id_valid(cid))
                {
                    return true;
                }
            }
        }
    }
    return false;

}
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried man but it is not working still validations are not taking place
Have you set a breakpoint on the first line inside validcourse? Just to see if your function ever gets executed?
You don't "write" breakpoints, you "set" them in your browser's debugger. In Chrome for example: developers.google.com/chrome-developer-tools/docs/…
0

Looks like you have a syntax error in your third functioncourse_duration_valid(course_duration)

retrun false; 

should be:

return false; 

1 Comment

ya spelling mistake i have made it return
0

Okay, so I got your stuff to work. Here are the changes needed:

First, object references need to be cleaner in your DOM

var course_name=document.getElementById("cname");
var course_duration=document.getElementById("cd");
var course_id=document.getElementById("cid");

As you can see, the elements need to have their individual ids.

Course Name:
<input type="text" name="coursename" id="cname" /><br>
Duration:
<input type="text" name="cd" id="cd"/><br>
Course Id:
<input name="cid" type="text" id="cid" /><br>

Finally, the retrun needed to be corrected, as already pointed out by @Steve

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.