1

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))">
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
PROFESSOR
  • 884
  • 3
  • 13
  • 20
  • @PROFESSOR, you title/short description is not good, you should also use code format .. and before everything else I'd recommend you read the FAQ. – ant Mar 10 '10 at 14:32
  • You need to edit the post to make the code more readable..also, the error output would probably be helpful. I don't see any semicolons, which would cause syntax errors, so if you don't have those in your code, you should add them in –  Mar 10 '10 at 14:33
  • Semicolons in JS aren't mandatory, but causes code harder to interpret (you don't know where the statement really ends), and leads to obscure bugs. – kennytm Mar 10 '10 at 14:35
  • You're passing `this` to functions which don't specify any parameters. – jackbot Mar 10 '10 at 14:37
  • @KennyTM: and it is absolutely death to optimizers and minifiers to leave out the semicolons. – Robusto Mar 10 '10 at 14:38

5 Answers5

1
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
  • 12,418
  • 6
  • 43
  • 61
  • Not required at all. Just best practice. – Quentin Mar 10 '10 at 14:58
  • 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 Mar 10 '10 at 15:29
1

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
  • 22,301
  • 5
  • 63
  • 96
  • 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. – Quentin Mar 10 '10 at 15:00
0

Use:

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

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

Michael D. Irizarry
  • 6,186
  • 5
  • 30
  • 35
  • 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. – Quentin Mar 10 '10 at 15:01
-1

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
  • 1,200
  • 6
  • 16
-1

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
  • 634
  • 4
  • 10
  • 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. – Quentin Mar 10 '10 at 15:03