Jump to content


Photo

Checking For Integer


  • Please log in to reply
17 replies to this topic

#1 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 28 November 2012 - 05:53 AM

I kinda need some help with a form. I need some Javascript to check if something submitted is an integer. I've googled it but the results I got looked unnecessarily complex.

Here's some code 'n stuff.

<script type="text/javascript">
    
function validatePage() {
    var msg="";

    if (document.forms.CustomerInformation.Title.value == "Please Choose") {
    msg += "* Please provide a Title\n";
    } else {
        msg += "";
    }
    if (document.forms.CustomerInformation.Firstname.value == "") {
    msg += "* Please provide a First Name\n";
    }
    if (document.forms.CustomerInformation.Lastname.value == "") {
    msg += "* Please provide a Surname\n";
    }    
    if (document.forms.CustomerInformation.Nationality.value == "") {
    msg += "* Please provide a Nationality\n";
    }	    
    if (document.forms.CustomerInformation.Issuecountry.value == "") {
    msg += "* Please provide a Country of Issue\n";
    }
    if (document.forms.CustomerInformation.Fullname.value == "") {
    msg += "* Please provide a Full name for your next of kin\n";
    }    
    if (document.forms.CustomerInformation.Address.value == "") {
    msg += "* Please provide an Address for your next of kin\n";
    }
    if (document.forms.CustomerInformation.Postcode.value == "") {
    msg += "* Please provide a Postcode for your next of kin\n";
    }    
    if (document.forms.CustomerInformation.Relationship.value == "") {
    msg += "* Please give details of your Relationship to your next of kin\n";
    }    
    if (document.forms.CustomerInformation.Mobilenumber.value == "") {
    msg += "* Please provide a Mobile Number for your next of kin\n";
    }    
    if (document.forms.CustomerInformation.checkbox.checked == false) {
    msg += "\n* Please read and agree to the Terms and Conditions\n";
    }
    
    if (msg == "") {
    return true;            
    } else {
    alert("This form cannot be submitted. Please correct the following issue(s):\n\n"+ msg);
    return false;
    }
    }
       
       
</script>

I want to test the "Mobilenumber" value for an integer. Any help would be appreciated.

#2 haku

haku

    Advanced Member

  • Staff Alumni
  • 6,172 posts

Posted 28 November 2012 - 07:10 AM

if (document.forms.CustomerInformation.Mobilenumber.match(/\D/)) {

        // A non-numeric character was entered

}


#3 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 28 November 2012 - 08:39 AM

if (document.forms.CustomerInformation.Mobilenumber.match(/\D/)) {
// A non-numeric character was entered
}


I tried that, and now the form just skips validation altogether and just submits to my database.

#4 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 28 November 2012 - 10:54 AM

function is_numeric(a) {

    var e = /^[0-9]+$/;

    if ((a.value.match(e)) {

        return true;

    }

    return false;

}

Edited by mrMarcus, 28 November 2012 - 10:55 AM.


#5 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 28 November 2012 - 11:30 AM

I see how that would work, but I do not see how I could integrate that into my code. I rarely use Javascript and although this is basic stuff, I have no idea how to integrate it.

#6 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 28 November 2012 - 11:35 AM

Something like:

function is_numeric(a) {
    var e = /^[0-9]+$/;
    if (a.match(e)) {
        return true;
    }
    return false;
}

function validatePage() {
    ...

    var mobile = document.forms.CustomerInformation.Mobilenumber.value;
    if ((mobile == "") || !is_numeric(mobile)) {
        msg += "* Please provide a Mobile Number for your next of kin\n";
    }

    ...
}

EDIT: Had typo in code. Updated code block.

Edited by mrMarcus, 28 November 2012 - 11:45 AM.


#7 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 28 November 2012 - 11:46 AM

Anything I add to my validatePage() function just makes it completely skip all validation. I just tried your code and while I am sure that it would work, it completely ruins my whole validation. Any ideas as to why?

#8 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 28 November 2012 - 11:59 AM

For development, remove the form submit() action and only have your script fire the validatePage() function. Do you have Firefox Web Developer or Firebug running? Makes for easy debugging.

Can you post your form? I will try try and replicate your issue locally.

EDIT: Also, please post your latest validatePage() function code.

Edited by mrMarcus, 28 November 2012 - 12:00 PM.


#9 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 28 November 2012 - 12:04 PM

The whole page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<?php include("includeDOB.php"); ?>
<title>Form</title>
<link type="text/css" rel="stylesheet" href="style2.css">
</head>
<body>



<script type="text/javascript">


function validatePage() {
var msg="";

if (document.forms.CustomerInformation.Title.value == "Please Choose") {
msg += "* Please provide a Title\n";
} else {
msg += "";
}
if (document.forms.CustomerInformation.Firstname.value == "") {
msg += "* Please provide a First Name\n";
}
if (document.forms.CustomerInformation.Lastname.value == "") {
msg += "* Please provide a Surname\n";
}
if (document.forms.CustomerInformation.Nationality.value == "") {
msg += "* Please provide a Nationality\n";
}	
if (document.forms.CustomerInformation.Issuecountry.value == "") {
msg += "* Please provide a Country of Issue\n";
}
if (document.forms.CustomerInformation.Fullname.value == "") {
msg += "* Please provide a Full name for your next of kin\n";
}
if (document.forms.CustomerInformation.Address.value == "") {
msg += "* Please provide an Address for your next of kin\n";
}
if (document.forms.CustomerInformation.Postcode.value == "") {
msg += "* Please provide a Postcode for your next of kin\n";
}
if (document.forms.CustomerInformation.Relationship.value == "") {
msg += "* Please give details of your Relationship to your next of kin\n";
}
if (document.forms.CustomerInformation.Mobilenumber.value == "") {
msg += "* Please provide a Mobile Number for your next of kin\n";
}
if (document.forms.CustomerInformation.checkbox.checked == false) {
msg += "\n* Please read and agree to the Terms and Conditions\n";
}

if (msg == "") {
return true;
} else {
alert("This form cannot be submitted. Please correct the following issue(s):\n\n"+ msg);
return false;
}
}


</script>

<img class="pclogo" src="http://planetcruise.co.uk/images/header/nav/logo3_Rev%20A.jpg">


<form action="submit.php" method="post" name="CustomerInformation" onsubmit="return validatePage()">
<br><br><br>
<span class="span1">Booking Reference: <input type="text" name="Bookingreference"></span><br><br><br><br>


<div class="rightstuff"> <br><br><br><br> <select name="Title"><option selected>Please Choose</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Mr">Ms</option>
<option value="Dr">Dr</option>
</select>
<br>




<input type="text" name="Firstname"><br>
<input type="text" name="Middlename"><br>
<input type="text" name="Lastname"><br>						
<?php echo $form1; ?> <!--$form1 can be found in "includeDOB.php" --><br><br>


											
															
											
<br><br><br><input type="text" name="PassportNo"><br>
<?php echo $form2; ?><br> <!--$form2 can be found in "includeDOB.php" -->
<?php echo $form3; ?><br> <!--$form3 can be found in "includeDOB.php" -->
<input type="text" name="Nationality"><br>
<input type="text" name="Issuecountry"><br><br>




<!-- NEXT OF KIN INFO v -->
											
<br><br><input type="text" name="Fullname"><br>
<input type="text" name="Address"><br>
<input type="text" name="Postcode"><br>
<input type="text" name="Relationship"><br>
<input type="text" name="Mobilenumber"><br>
<input type="text" name="Daytimephone"><br>
<input type="text" name="Eveningphone"><br>
<input type="text" name="Email"><br><br>


											
											
<!--TRAVEL INSURANCE DETAILS-->
											
<br><br><br><input type="text" name="Company"><br>
<input type="text" name="Policynumber"><br>
<input type="text" name="Underwriter"><br>
<input type="text" name="Emergencynumber"><br><br><br></div>

<div class="leftstuff"><h1>Personal Information</h1><br>
<!--PERSONAL INFORMATION -->
			
											
Title:*<br>First Name:*<br>Middle Name:<br>Last Name:*<br>Last Name:*<br>Date of Birth:*<br><br><h1>Passport Details</h1><br>Passport Number:*<br>Date of Issue:*<br>Date of Expiry:*<br>Nationality:*<br>Issue Country:*<br><br><h1>Next of Kin Information</h1><br>Full Name:*<br>Address:*<br>Postcode:*<br>Relationship:*<br>Mobile Number:*<br>Daytime Phone:<br>Evening Phone:<br>Email Address:<br><br><h1>Travel Insurance Details</h1><br>Insurance Company:<br>Policy Number:<br>Medical Company Name:<br>Emergency Medical Contact Number:<br><br><br><span class="bottomtext">I have read and agree to the <a target="_blank" href="http://planetcruise.co.uk/about-planet-cruise/terms">terms and conditions</a></span> <input type="checkbox" name="checkbox"><br><br><input type="submit" value="Submit"><br><br></div>



		


</form>
</body>
</html>

includeDOB.php

<?php
$form1 = "<select name=\"day1\">\n";
$day1=1;
while($day1<32) {
$form1 .= "<option value=\"{$day1}\">{$day1}</option> ";
$day1++;
}
$form1 .= "</select>\n \n <select name=\"month1\">\n";
$month1 = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novermber", "December");
foreach ($month1 as $key) {
$form1 .= "<option value=\"{$key}\">{$key}</option>\n ";
}
$form1 .= "</select>\n \n <select name=\"year1\">\n";
$year1=1901;
while($year1<2012 && $year1 > 1900) {
$form1 .= "<option value=\"{$year1}\">{$year1}</option>\n ";
$year1++;
}
$form1 .= "</select> \n";
?>

<?php
$form2 = "<select name=\"day2\">\n";
$day2=1;
while($day2<32) {
$form2 .= "<option value=\"{$day2}\">{$day2}</option> ";
$day2++;
}
$form2 .= "</select>\n \n <select name=\"month2\">\n";
$month2 = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novermber", "December");
foreach ($month2 as $key) {
$form2 .= "<option value=\"{$key}\">{$key}</option>\n ";
}
$form2 .= "</select>\n \n <select name=\"year2\">\n";
$year2=1901;
while($year2<2012 && $year2 > 1900) {
$form2 .= "<option value=\"{$year2}\">{$year2}</option>\n ";
$year2++;
}
$form2 .= "</select> \n";
?>

<?php
$form3 = "<select name=\"day3\">\n";
$day3=1;
while($day3<32) {
$form3 .= "<option value=\"{$day3}\">{$day3}</option> ";
$day3++;
}
$form3 .= "</select>\n \n <select name=\"month3\">\n";
$month3 = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novermber", "December");
foreach ($month3 as $key) {
$form3 .= "<option value=\"{$key}\">{$key}</option>\n ";
}
$form3 .= "</select>\n \n <select name=\"year3\">\n";
$year3=1901;
while($year3<2012 && $year3 > 1900) {
$form3 .= "<option value=\"{$year3}\">{$year3}</option>\n ";
$year3++;
}
$form3 .= "</select> \n";
?>

Edited by Beeeeney, 28 November 2012 - 12:05 PM.


#10 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 28 November 2012 - 12:19 PM

I had no issues with it.

This is the JS block I used (cut out all the fluff as I didn't feel like filling out your form):

function is_numeric(a) {
var e = /^[0-9]+$/;
if (a.match(e)) {
return true;
}
return false;
}
function validatePage() {
    var msg="";

    var mobile = document.forms.CustomerInformation.Mobilenumber.value;
    if ((mobile == '') || !is_numeric(mobile)) {
        msg += "* Please provide a Mobile Number for your next of kin\n";
    }

    if (msg == "") {
        return true;
    } else {
        alert("This form cannot be submitted. Please correct the following issue(s):\n\n"+ msg);
        return false;
    }
}

Upon submitting your form it would die (stop) if I did not enter a numeric value for mobile, but would continue through (process) if I did.

#11 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 28 November 2012 - 12:43 PM

I even tested it with your updated code and for some reason it is still completely skipping validation.

<script type="text/javascript">
    

function is_numeric(a) {
var e = /^[0-9]+$/;
if (a.match(e)) {
return true;
}
return false;
}

var mobile = document.forms.CustomerInformation.Mobilenumber.value;
    
function validatePage() {
    var msg="";

    if (document.forms.CustomerInformation.Title.value == "Please Choose") {
    msg += "* Please provide a Title\n";
    } else {
        msg += "";
    }
    if (document.forms.CustomerInformation.Firstname.value == "") {
    msg += "* Please provide a First Name\n";
    }
    if (document.forms.CustomerInformation.Lastname.value == "") {
    msg += "* Please provide a Surname\n";
    }    
    if (document.forms.CustomerInformation.Nationality.value == "") {
    msg += "* Please provide a Nationality\n";
    }	    
    if (document.forms.CustomerInformation.Issuecountry.value == "") {
    msg += "* Please provide a Country of Issue\n";
    }
    if (document.forms.CustomerInformation.Fullname.value == "") {
    msg += "* Please provide a Full name for your next of kin\n";
    }    
    if (document.forms.CustomerInformation.Address.value == "") {
    msg += "* Please provide an Address for your next of kin\n";
    }
    if (document.forms.CustomerInformation.Postcode.value == "") {
    msg += "* Please provide a Postcode for your next of kin\n";
    }    
    if (document.forms.CustomerInformation.Relationship.value == "") {
    msg += "* Please give details of your Relationship to your next of kin\n";
    }    
    if ((mobile == "") || !is_numeric(mobile)) {
	    msg += "* Please provide a Mobile Number for your next of kin\n";
	    }
    if (document.forms.CustomerInformation.checkbox.checked == false) {
    msg += "\n* Please read and agree to the Terms and Conditions\n";
    }
    
    if (msg == "") {
    return true;            
    } else {
    alert("This form cannot be submitted. Please correct the following issue(s):\n\n"+ msg);
    return false;
    }
    }
       
       
</script>


#12 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 28 November 2012 - 12:58 PM

Honestly, I cannot replicate your issue. Works just fine on my end.

You're testing multiple ways, correct? E.g. submitting form completely empty, adding alpha characters to the mobile input, etc.

#13 haku

haku

    Advanced Member

  • Staff Alumni
  • 6,172 posts

Posted 28 November 2012 - 09:41 PM

The code I provided here:

if (document.forms.CustomerInformation.Mobilenumber.match(/\D/)) {
// A non-numeric character was entered
}


Does the exact same thing shown in the function is_numeric() shown here, but with less overhead.:

function is_numeric(a) {
var e = /^[0-9]+$/;
if ((a.value.match(e)) {
return true;
}
return false;
}

They are both using regex to test if the given value is entirely numeric or not.

I tried that, and now the form just skips validation altogether and just submits to my database.

Two points:
1) You probably have an error somewhere else in your javascript. If you turn on a javascript debugging console, you will be able to see any errors that pop-up.
2) You should not rely entirely on javascript validation to ensure your code is not submitted to the database, as some users will have javascript turned on. Javascript should be used as an addition to server-side validation, not in place of it.

Edited by haku, 28 November 2012 - 09:48 PM.


#14 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 05 December 2012 - 05:56 AM

I do have an error, apparently:

Uncaught TypeError: Cannot read property 'Mobilenumber' of undefined

No reason for this error to be here.

#15 haku

haku

    Advanced Member

  • Staff Alumni
  • 6,172 posts

Posted 06 December 2012 - 01:18 AM

There is most definitely a reason, or you would not be seeing it! The question is what is causing that error.

In your code, the only place you are using Mobilenumber is here:
if (document.forms.CustomerInformation.Mobilenumber.value == "") {

The error is saying that document.forms.CustomerInformation is undefined. This is strange seeing as you would see this error much earlier than this spot, since you are using document.forms.CustomerInformation in plenty of the code before that. This leads me to think that maybe you aren't showing us the actual code you are using, is that correct? If so, can you please post your current/actual code?

#16 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 06 December 2012 - 03:39 AM

I showed you the actual code I was using, but I'll fire up my editor and show you the exact code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<?php include("includeDOB.php"); ?>
<title>Form</title>
<link type="text/css" rel="stylesheet" href="style2.css">
</head>
<body>



<script type="text/javascript">


function is_numeric(a) {
var e = /^[0-9]+$/;
if (a.match(e)) {
return true;
}
return false;
}

var mobile = document.forms.CustomerInformation.Mobilenumber.value;

function validatePage() {
var msg="";

if (document.forms.CustomerInformation.Title.value == "Please Choose") {
msg += "* Please provide a Title\n";
} else {
msg += "";
}
if (document.forms.CustomerInformation.Firstname.value == "") {
msg += "* Please provide a First Name\n";
}
if (document.forms.CustomerInformation.Lastname.value == "") {
msg += "* Please provide a Surname\n";
}
if (document.forms.CustomerInformation.Nationality.value == "") {
msg += "* Please provide a Nationality\n";
}	
if (document.forms.CustomerInformation.Issuecountry.value == "") {
msg += "* Please provide a Country of Issue\n";
}
if (document.forms.CustomerInformation.Fullname.value == "") {
msg += "* Please provide a Full name for your next of kin\n";
}
if (document.forms.CustomerInformation.Address.value == "") {
msg += "* Please provide an Address for your next of kin\n";
}
if (document.forms.CustomerInformation.Postcode.value == "") {
msg += "* Please provide a Postcode for your next of kin\n";
}
if (document.forms.CustomerInformation.Relationship.value == "") {
msg += "* Please give details of your Relationship to your next of kin\n";
}
if ((mobile == "") || !is_numeric(mobile)) {
msg += "* Please provide a Mobile Number for your next of kin\n";
}
if (document.forms.CustomerInformation.checkbox.checked == false) {
msg += "\n* Please read and agree to the Terms and Conditions\n";
}

if (msg == "") {
return true;
} else {
alert("This form cannot be submitted. Please correct the following issue(s):\n\n"+ msg);
return false;
}
}


</script>

<img class="pclogo" src="http://planetcruise.co.uk/images/header/nav/logo3_Rev%20A.jpg">


<form action="submit.php" method="post" name="CustomerInformation" onsubmit="return validatePage()">
<br><br><br>
<span class="span1">Booking Reference: <input type="text" name="Bookingreference"></span><br><br><br><br>


<div class="rightstuff"> <br><br><br><br> <select name="Title"><option selected>Please Choose</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Mr">Ms</option>
<option value="Dr">Dr</option>
</select>
<br>




<input type="text" name="Firstname"><br>
<input type="text" name="Middlename"><br>
<input type="text" name="Lastname"><br>						
<?php echo $form1; ?> <!--$form1 can be found in "includeDOB.php" --><br><br>


											
															
											
<br><br><br><input type="text" name="PassportNo"><br>
<?php echo $form2; ?><br> <!--$form2 can be found in "includeDOB.php" -->
<?php echo $form3; ?><br> <!--$form3 can be found in "includeDOB.php" -->
<input type="text" name="Nationality"><br>
<input type="text" name="Issuecountry"><br><br>




<!-- NEXT OF KIN INFO v -->
											
<br><br><input type="text" name="Fullname"><br>
<input type="text" name="Address"><br>
<input type="text" name="Postcode"><br>
<input type="text" name="Relationship"><br>
<input type="text" name="Mobilenumber"><br>
<input type="text" name="Daytimephone"><br>
<input type="text" name="Eveningphone"><br>
<input type="text" name="Email"><br><br>


											
											
<!--TRAVEL INSURANCE DETAILS-->
											
<br><br><br><input type="text" name="Company"><br>
<input type="text" name="Policynumber"><br>
<input type="text" name="Underwriter"><br>
<input type="text" name="Emergencynumber"><br><br><br></div>

<div class="leftstuff"><h1>Personal Information</h1><br>
<!--PERSONAL INFORMATION -->
			
											
Title:*<br>First Name:*<br>Middle Name:<br>Last Name:*<br>Last Name:*<br>Date of Birth:*<br><br><h1>Passport Details</h1><br>Passport Number:*<br>Date of Issue:*<br>Date of Expiry:*<br>Nationality:*<br>Issue Country:*<br><br><h1>Next of Kin Information</h1><br>Full Name:*<br>Address:*<br>Postcode:*<br>Relationship:*<br>Mobile Number:*<br>Daytime Phone:<br>Evening Phone:<br>Email Address:<br><br><h1>Travel Insurance Details</h1><br>Insurance Company:<br>Policy Number:<br>Medical Company Name:<br>Emergency Medical Contact Number:<br><br><br><span class="bottomtext">I have read and agree to the <a target="_blank" href="http://planetcruise.co.uk/about-planet-cruise/terms">terms and conditions</a></span> <input type="checkbox" name="checkbox"><br><br><input type="submit" value="Submit"><br><br></div>



		


</form>
</body>
</html>

Edited by Beeeeney, 06 December 2012 - 03:40 AM.


#17 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,576 posts
  • LocationBonita, FL

Posted 06 December 2012 - 05:30 AM

var mobile = document.forms.CustomerInformation.Mobilenumber.value;

function validatePage() {


That variable definition needs to be inside your validatePage function.  By placing it outside, you are attempting to get that field's value as the page loads, which due to the placement of the script, happens before the field or the form exists yet.  Even if it did work, it would only get the value of the field when the page loaded (ie, blank), not the value after someone entered their data.

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#18 Beeeeney

Beeeeney

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationEngland!
  • Age:20

Posted 06 December 2012 - 07:27 AM

It works now! Thanks a lot. :)

Edited by Beeeeney, 06 December 2012 - 07:29 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com