开发者

my javascript is not working

my javascript is not validating

<script type="text/javascript">
    function checkname()
    {
        var str1=document.form.name.value

        if (str1.length==0)
        {
            return true
        }
        else{
            alert('Please Enter Your Name!')
            return false
        }
    }

    function checkrollno()
    {
        var ph=document.form.rollno.value
        var l=ph.length

        if((ph==""||ph==null))
        {
            alert('Enter the University Roll Number')
            return false 
        }

        if(l&l开发者_运维问答t;10)
        {
            alert('Roll no. Consists Of 10 Digits ')
            return false
        }

        return true
    }

    function checkPercent()
    {
        var name1=document.form.btech.value
        var name2=document.form.12.value

        if(name1==null||name1==""||name2==null||name2=="")
        {
            alert('Enter The Percentage')
            return false
        }

        return true
    }
</script>

and the form looks like

<form name="form" method="post" action="action.php" onSubmit="return (checkname(this) && checkrollno(this) && checkPercent(this))">


alert('Roll no. Consists Of 10 Digits ');
return false;

Use ; after each and every statement. (Not required if there is only 1 statement in a block, but still be on safe side)


You're passing 'this' into each of the functions, yet none of the functions accept/ expect an argument, so you can drop that. Add semicolons to the end of each statement. And you need to rename one of the forms on your page (or you have a typo) as you can't have a variable/ property in JavaScript that begins with a number.

var name2=document.form.12.value

Though I suppose you could try

var name2=document.form['12'].value;

but please don't. Also, where you have

var str1=document.form.name.value

.name should be the name of your form (which appears to be 'form', not 'name' though you seem to have multiple forms).


Use:

var str1=document.forms[0].name.value

But for next time follow the so rules :) and try JQuery.


Change the form tag for something in the like of

<form name="form" method="post" action="action.php" onSubmit="ValidateAll()">

And create a new function:

function ValidateAll()
{ 
     return checkname() && checkrollno() && checkPercent()
}


The problem is with these lines

var str1=document.form.name.value

var ph=document.form.rollno.value

etc.

Make sure you put a ; at the end of each line as javascript doesn't treat these as EOL.

var str1=document.form.name.value;

var ph=document.form.rollno.value;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜