Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts

    alert on wrong data type

    how would i go about making a script to make sure that in certain fields they can only enter numbers for example in the # of siblings field, they can only enter 1 or 2 and so on and so forth, but needs to work for multi fields and fields will need there own names so i can feed info onto nextpage
    thanks rafiki

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    And the point of making a new thread was? Stick to your original threads when the question you have is the same question. Don't waste our time. alert on wrong data type
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    sorry couldnt find it

  • #4
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    didnt i give you some code that work the other day for validating?

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    yes but couldnt figure it out if i repost code and a more indepth explanation of wat i need you think u could help me fix it up?

  • #6
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah sure

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    this is wat aero gave me
    Code:
    <script type="text/javascript">
    function isNum()
    {
    	var thetest = document.forms[0].siblings;
    	thetest.setAttribute('autocomplete','off');
    	if(!thetest.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers');
    		thetest.value = '';
    		thetest.focus();
    	}
    }
    </script>
    and i need it to check that the field siblings is a number aswell as time you wake up, the field name is "siblings" and "wakeup" named accordingly

  • #8
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    k hold on a sec

  • #9
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that script before didnt work here i one that you dont need to edit it i think

    Code:
    <head>
    <title>Javascript Sample Page</title>
    <script language="JavaScript">
    <!--  to hide script contents from old browsers
    function VerifyData() {
    // Source code to check form data goes here.
    // Create a variable to keep track of whether the form is valid.
    // Initializing this value to 1 means, in effect, that
    // the form is valid unless the value changes sometime in the
    // routine.
    var valid = 1
    if (document.Customer.FirstLast.value == "") {
    		valid = 0
    	}
    if (document.Customer.Email.value == "") {
    		valid = 0
    	}
    if (!CheckPhoneNumber(document.Customer.Phone.value)) {
    		valid = 0
    	}
    // Here we decide whether to submit the form.
    	if (!valid) {
    alert("Please complete all the form fields and enter a valid  number.")
    	}
    	return valid
    	}
    
    function CheckPhoneNumber(TheNumber) {
    var valid = 1
    var GoodChars = "0123456789 "
    var i = 0
    if (TheNumber=="") {
    // Return false if number is empty
    valid = 0
    	}
    for (i =0; i <= TheNumber.length -1; i++) {
    if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
    // Note: Remove the comments from the following line to see this
    // for loop in action.
    // alert(TheNumber.charAt(i) + " is no good.")
    valid = 0
    } // End if statement
    	} // End for loop
    return valid
    }
    // end hiding contents from old browsers  -->
    </script>
    <link rel="SHORTCUT ICON" href="http://webdeveloper.com/favicon.ico">
    </head>
    <body>
    <h2>Sample page for JavaScript Tutorial</h2>
    Press the submit button to verify form data.
    <p>
    <form name="Customer" action=""
    method="POST" onsubmit="return VerifyData()">
    Your name:
    <input type="text" name="FirstLast" size="30" maxlength="75" value=""> <br>
    Your email:
    <input type="text" name="Email" size="30" maxlength="75" value=""> <br>
    bedtime:
    <input type="text" name="Phone" size="30" maxlength="75" value=""> <br>
    <input type="submit">
    <input type="reset">
    </form>
    <p>
    </body>
    </html>
    to create a new form just add }
    if (!Checkformname(document.Customer.formname.value)) {
    valid = 0
    }
    let me know if you have probs

  • #10
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    thanks google but im not sure its working, maybe just not working in ff

  • #11
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    The script I provided does work or I would not have provided it. It was tested. Look try this
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function isNum()
    {
    	var test1 = document.forms[0].siblings;
    	var test2 = document.forms[0].wakeup;
    	if(!test1.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the siblings field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    	}
    	else if(!test2.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the wakeup field.');
    		test2.value = '';
    		test2.focus();
    		return false;
    	}
    }
    </script>
    </head>
    
    <body>
    <form action="#" method="post" onsubmit="return isNum()">
    <label for="siblings">Siblings: <input type="text" name="siblings" id="siblings></label>
    <label for="wakeup">Wakeup: <input type="text" name="wakeup" id="wakeup"></label>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    Enter numbers and letters in the boxes then press submit. You will get an alert if you entered letters in any of the two boxes.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    thanks it working but i tried to make more fields and it all went pete tong
    Code:
    ><script type="text/javascript">
    function isNum()
    {
    	var test1 = document.forms[0].siblings;
    	var test2 = document.forms[0].wake;
                          var test3 = document.forms[0].homework;
                          var test4 = document.forms[0].computer;
                           var test5 = document.forms[0].tv;
                           var test6 = document.forms[0].friends;
                           var test7 = document.forms[0].family;
    
    	if(!test1.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the siblings field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    	}
    	else if(!test2.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the wakeup field.');
    		test2.value = '';
    		test2.focus();
    		return false;
    	}
                      else if(!test3.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the homework field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    }
      else if(!test4.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the computer field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    }
                         else if(!test5.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the tv field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    }
      else if(!test6.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the friends field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    }
      else if(!test3.value.match(/^\d+$/))
    	{
    		alert('You can only enter numbers in the family field.');
    		test1.value = '';
    		test1.focus();
    		return false;
    }
    
    
    </script>

  • #13
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    How about you post the form you are using so we can stop guessing how you need the script to be. First you say 1 field, then you say 2 fields, now you say like 6 fields. Make up your mind.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #14
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    sorry i thought it would be pretty easy to edit myself guess i was wrong
    Code:
    <form action="welcome.php" method="post" onsubmit="return isNum()">
    First Name: <input type="text" name="firstname" /><br />
    Surname: <input type="text" name="surname" /><br />
    DOB: <input type="text" name="DOB" /><br />
    Year In School:
    <select name="school_year">
    <option value="7" />7
    <option value="8" />8
    <option value="9" />9
    <option value="10" />10
    <option value="11" />11
    </select><br />
    <label for="siblings">Number of Siblings: <input type="numeric" name="siblings" id="siblings" />*</label><br />
    Bedtime: <select name="bedtime" ><br />
    <option value="7" />7
    <option value="8" />8
    <option value="9" />9
    <option value="10" />10
    <option value="11" />11
    </select><br />
    <label for="wake">Time You Wake Up:<input type="text" name="wake" id="wake" />*</label>
    <br />
    <label for="homework">Hours Doing Home Work: <input type="text" name="homework" id="homework" />*<br /></label>
    <label for="tv">Hours watching T.V: <input type="text" name="tv" id="tv" />|*<br /></label>
    <label for="computer">Hours Playing Computer/console: <input type="text" name="computer" id="computer" />*<br /></label>
    <label for="family">Hours Spent With Family: <input type="text" name="family id="family" " />*<br /></label>
    <label for="friends">Hours Spent With Friends: <input type="text" name="friends" id="friends" />*<br /></label>
    <input type="submit" />
    </form>

  • #15
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <
    title>Untitled Document</title>
    <
    script type="text/javascript">
    //add the field names to the the line below making sure they are in the same order as they appear on the page
    var fields = new Array("siblings","wake","homework","tv","computer","family","friends");
    //this is what the user will see if a field is filled in incorrectly, match them with the corresponding names
    var labels = new Array("Number of Siblings","Time You Wake Up","Hours Doing Home Work","Hours watching T.V","Hours Playing Computer/console","Hours Spent With Family","Hours Spent With Friends");
    var 
    error1 '';
    var 
    error2 '';
    function 
    isNum()
    {
        for(var 
    0fields.lengthi++)
        {
            if(!
    document.forms[0].elements[fields[i]].value.match(/^d+$/) && document.forms[0].elements[fields[i]].value != '')
            {
                
    error1 += "\n"+labels[i];
            }
            if(
    document.forms[0].elements[fields[i]].value == '')
            {
                
    error2 += "\n"+labels[i];
            }
        }
        if(
    error1 != '')
        {
            
    alert("You cannot enter numbers in the following fields:\n"+error1);
            
    error1 '';
            return 
    false;
        }
        if(
    error2 != '')
        {
            
    alert("You cannot leave the following fields blank:\n"+error2);
            
    error2 '';
            return 
    false;
        }
    }
    </script>
    </head>
    <body>
    <form action="#" method="post" onsubmit="return isNum()">
        <label for="firstname">First Name: <input type="text" name="firstname" id="firstname" /><br /></label>
        <label for="surname">Surname: <input type="text" name="surname" id="surname" /><br /></label>
        <label for="DOB">DOB: <input type="text" name="DOB" id="DOB" /><br /></label>
        <label for="school_year">Year In School:
        <select name="school_year" id="school_year">
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
        </select>
        <br />
        </label>
        <label for="siblings">Number of Siblings: <input type="text" name="siblings" id="siblings" />*<br /></label>
        <label for="bedtime">Bedtime:
        <select name="bedtime" id="bedtime">
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
        </select>
        <br />
        </label>
        <label for="wake">Time You Wake Up: <input type="text" name="wake" id="wake" />*<br /></label>
        <label for="homework">Hours Doing Home Work: <input type="text" name="homework" id="homework" />*<br /></label>
        <label for="tv">Hours watching T.V: <input type="text" name="tv" id="tv" />*<br /></label>
        <label for="computer">Hours Playing Computer/console: <input type="text" name="computer" id="computer" />*<br /></label>
        <label for="family">Hours Spent With Family: <input type="text" name="family" id="family" />*<br /></label>
        <label for="friends">Hours Spent With Friends: <input type="text" name="friends" id="friends" />*<br /></label>
        <input type="submit" value="Submit" />
    </form>
    </body>
    </html> 
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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