SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2003
    Location
    England!
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Listing multiple variables

    Im validating a form, i have set the name, surname, house number etc etc (total of 7) as variables, im now checking the fields to ensure there is a data entry in these fields.

    Rather going through to check each IF statement individually (like below) is there a way for me to check all of them at once, instead of 7 different IF statements to check the field?

    Say where the "forename" variable is, could i say add all 7 at once to check? Obviously this will make things neater and cut down on the amount of code

    Code:
    if (forename == "")
    	{
    	alert("The Forename Field Has No Value")
    	return true
    	}
    else
    	{
    	return false
    	}

  2. #2
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll have to use an array or some form of code branching if you want the alert to be field-specific. However, you could check that no field is empty using a single statement with the aid of the OR || operator.

    E.g.
    Code:
    if (forename=="" || surname=="" || houseNumber=""){
     alert("Please complete all fields.")
     return true
    }
    else{
     return false
    }

    Andy
    From the English nation to a US location.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •