function validscript()
{
    var fname = document.getElementById("fname") ;
    var lname = document.getElementById("lname") ;
    var number = document.getElementById("number") ;
    var email = document.getElementById("email") ;
    var confemail = document.getElementById("confemail") ;
    var street = document.getElementById("street") ;
    var city = document.getElementById("city") ;
    var state = document.getElementById("state") ;
    var zip = document.getElementById("zip") ;

    if (fname.value ="")
    {
        alert ("Please enter a valid first name.");
    }

    if (lname.value ="")
    {
        alert ("Please enter a valid last name.");
    }

    if (number.value ="")
    {
        alert ("Please enter a valid phone number.") ;
    }

    if (email.value ="")
    {
        alert ("Please enter a valid email address.") ;
    }

    if (confemail.value ="")
    {
        alert ("Please enter a valid confirmation email address.") ;
    }

    if (street.value ="")
    {
        alert ("Please enter a valid street address.") ;
    }

    if (city.value ="")
    {
        alert ("Please enter a valid city.") ;
    }

    if (state.value ="")
    {
        alert ("Please select a state.") ;
    }

    if (zip.value ="")
    {
        alert ("Please enter a valid zip code.") ;
    }

if (alert () == "")
{
    return false;
}
    else
    {
        return true;
    }


var boxesChked = ""


    for (i = document.getElementsByName('sr[]').length - 1; i >= 0; i--)
    {
        if (document.getElementsByName('sr[]')[i].checked)
            {
                boxesChked = boxesChked + document.getElementsByName('sr[]')[i].value + "\n"
            }
    }
if (boxesChked == "")
{
    alert ("You must select a box to continue.");

    return false;
}
    else
    {
        <!--alert (boxesChked)-->

        return true;
    }
}

It still allows the form to be submited and none of the alerts show up.

The alert only shows a message, you cannot check it's value. Use a separate variable to store true/false to return as the function result.

Here is an example of JavaScript code you can use to validate a form:

<script type="text/javascript">
function validateRequired()
{
var x=document.forms["form1"]["name"].value;
if (x==null || x=="")
  {
  alert("Name cannot be blank.");
  return false;
  }
}
</script>

<form name="form1" action="process.aspx" 
         onsubmit="return validateRequired()" method="post">
    Name: <input type="text" name="name">
    <input type="submit" value="Submit">
</form>

You know another way to do this, is if you are using dreamweaver and If not already installed, use spry framework for ajax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.