2 Replies - 2946 Views - Last Post: 16 April 2010 - 11:31 AM

#1 crafty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 10-May 08

prevent submit on empty input - validation problem

Posted 14 April 2010 - 05:32 AM

<script language = "Javascript">
        function echeck(str) {

            var at="@"
            var dot="."
            var lat=str.indexOf(at)
            var lstr=str.length
            var ldot=str.indexOf(dot)
            if (str.indexOf(at)==-1){
                alert("Invalid Email")
                return false
            }

            if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                alert("Invalid Email")
                return false
            }

            if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                alert("Invalid Email")
                return false
            }

            if (str.indexOf(at,(lat+1))!=-1){
                alert("Invalid Email")
                return false
            }

            if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                alert("Invalid Email")
                return false
            }

            if (str.indexOf(dot,(lat+2))==-1){
                alert("Invalid Email")
                return false
            }

            if (str.indexOf(" ")!=-1){
                alert("Invalid Email")
                return false
            }

           return true
        }

        function Validate(){
            var email=document.successfrm.email
            var batchid=document.successfrm.batchid
            var sname=document.successfrm.sname

            if ((sname.value==null)||(sname.value=="")){
                alert("Please enter the Student Name")
                user.focus()
                return false
            }
            if ((batchid.value==null)||(batchid.value=="")){
                alert("Please enter the Batch ID")
                pass.focus()
                return false
            }

            if ((email.value==null)||(email.value=="")){
                alert("Please enter your Email")
                email.focus()
                return false
            }
            if (echeck(email.value)==false){
                email.value=""
                email.focus()
                return false
            }
            return true
        }
    </script>


<form name="successfrm" action="success" method="Post" onsubmit="return Validate(this);">
                                 <table align="center" frame="border" >
                                    <tr>
                                        <td>Student Name </td>
                                        <td><input type="text" name="sname" value="" /></td>

                                    </tr>
                                    <tr>
                                        <td>Batch ID </td>
                                        <td><input type="text" name="batchid" value="" /></td>
                                    </tr>
                                    <tr>
                                        <td>Email</td>
                                        <td><input type="text" name="email" value="" /></td>
                                    </tr>
                                    <tr>
                                        <td><input type="submit" value="Submit" name="submit" /></td>
                                        <td>  <input type="reset" value="Reset" name="reset" /></td>
                                    </tr>
                                </table>

                            </form>


The form submits even if no input is provided. The database get updated with blank values. Please help me.

This post has been edited by crafty: 14 April 2010 - 05:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: prevent submit on empty input - validation problem

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: prevent submit on empty input - validation problem

Posted 14 April 2010 - 10:10 AM

You should probably adjust your server-side processing code to validate the input that's been passed to it, as well as tweak your javascript - as it stands, a user with javascript disabled will just submit your form with empty values and effortlessly bypass your validation.

That being said, you may want to adjust your validation code to work something like this:
function Validate(){
    var email=document.successfrm.email;
    var batchid=document.successfrm.batchid;
    var sname=document.successfrm.sname;
		var something_went_wrong = false;

    if ((sname.value==null)||(sname.value=="")){
        alert("Please enter the Student Name");
        user.focus();
				something_went_wrong = true;
    }
    if ((batchid.value==null)||(batchid.value=="")){
        alert("Please enter the Batch ID");
        pass.focus();
        something_went_wrong = true;
    }

    if ((email.value==null)||(email.value=="")){
        alert("Please enter your Email");
        email.focus();
        something_went_wrong = true;
    }
    if (echeck(email.value)==false){
        email.value="";
        email.focus();
        something_went_wrong = true;
    }
		if (something_went_wrong) {
			return false;
		} else {
			return true;
		}
}


Was This Post Helpful? 1
  • +
  • -

#3 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: prevent submit on empty input - validation problem

Posted 16 April 2010 - 11:31 AM

In your Validate function you have this:
var email=document.successfrm.email
            var batchid=document.successfrm.batchid
            var sname=document.successfrm.sname

            if ((sname.value==null)||(sname.value=="")){
                alert("Please enter the Student Name")
                user.focus()
                return false
            }
            if ((batchid.value==null)||(batchid.value=="")){
                alert("Please enter the Batch ID")
                pass.focus()
                return false
            }

            if ((email.value==null)||(email.value=="")){
                alert("Please enter your Email")
                email.focus()
                return false
            }



Which is fine. What is breaking it is the fact that you are trying to set focus to user and pass for sname and batchid respectively. You can see what I'm talking about here:
if ((sname.value==null)||(sname.value=="")){
        alert("Please enter the Student Name");
        user.focus();  // Needs to be sname.focus();
                                something_went_wrong = true;
    }
    if ((batchid.value==null)||(batchid.value=="")){
        alert("Please enter the Batch ID");
        pass.focus();  // Needs to be batchid.focus();
        something_went_wrong = true;
    }



:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1