tags:

views:

89

answers:

5

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<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))">
+1  A: 
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)

N 1.1
Not required at all. Just best practice.
David Dorward
But in later stages of development, if you use minifiers/compressors, it will throw errors. And to let everything to guess for browser is not good :). http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript
N 1.1
+1  A: 

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).

Tom
In that last example `form` is the name of the form, and shouldn't be confused with the `forms` collection. It is generally better to use `document.forms.sensibleId.elements.otherSensibleIdOrName` to avoid conflicts and for clarity.
David Dorward
A: 

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()
}
jpabluz
There must a return for onSubmit -- onSubmit="return ValidateAll()"
GTM
A: 

Use:

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

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

JeremySpouken
Avoid accessing forms by numeric index. It is the best way for things to break when the page layout changes. Using a name (which is what the OP is already doing) is a better bet.
David Dorward
A: 

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;
GTM
nvi said that 20 minutes previously, and it is still wrong. JavaScript performs semi-colon insertation, it is a good way to trip yourself up, and it is best avoided, but it isn't actually a problem here.
David Dorward