...

View Full Version : alert on wrong data type



rafiki
09-02-2006, 05:21 PM
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

_Aerospace_Eng_
09-02-2006, 06:55 PM
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. http://www.codingforums.com/showthread.php?t=94972

rafiki
09-02-2006, 08:04 PM
sorry couldnt find it

googleit
09-02-2006, 08:33 PM
didnt i give you some code that work the other day for validating?

rafiki
09-02-2006, 08:45 PM
yes but couldnt figure it out :confused: if i repost code and a more indepth explanation of wat i need you think u could help me fix it up?

googleit
09-02-2006, 10:13 PM
yeah sure :)

rafiki
09-03-2006, 02:02 PM
this is wat aero gave me
<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 :)

googleit
09-03-2006, 02:12 PM
k hold on a sec :)

googleit
09-03-2006, 02:30 PM
that script before didnt work here i one that you dont need to edit it i think:rolleyes:


<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 :)

rafiki
09-03-2006, 09:37 PM
thanks google but im not sure its working, maybe just not working in ff

_Aerospace_Eng_
09-03-2006, 10:46 PM
The script I provided does work or I would not have provided it. It was tested. Look try this

<!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.

rafiki
09-03-2006, 11:49 PM
thanks it working but i tried to make more fields and it all went pete tong

><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>

_Aerospace_Eng_
09-04-2006, 12:12 AM
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.

rafiki
09-04-2006, 12:18 AM
sorry i thought it would be pretty easy to edit myself guess i was wrong

<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>

_Aerospace_Eng_
09-04-2006, 01:22 AM
<!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 i = 0; i < fields.length; i++)
{
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>

rafiki
09-04-2006, 02:05 AM
spot on thanks, could you take a look at anouther thread please?
http://codingforums.com/showthread.php?t=95178
also is it possible for me 2 nominate for u aswell as googleit?

googleit
09-04-2006, 04:01 PM
nope you already have done but hanks anyways :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum