Problem in validating number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Problem in validating number

Post by lala »

Hi, I had pass value's problem when validate number in PHP.
I do not know what error cause function cannot work
Hope someone can help me, thanks..

this is field.php

Code: Select all

<HEAD>
 <TITLE>Number of Fields</TITLE>
</HEAD>

<script language="javascript">

function trim(str)
{
 return str.replace(/^\s*|\s*$/g,"");
}

function number(tf)
{
 for (x=0;x<tf.length;x++)
 {
  if (!isDigit(tf.charAt(x)))
  {
   return false;
  }
 }
 return true;
}

function isDigit(dig)
{
 return((dig<="9")&&(dig>="0")||dig==".");
}


function validateForm()
{
var validate=true;
 var f=document.field;

 if (trim(f.no_field.value)=="" && validate)
 {
  validate=false
  alert("Please enter Number of Fields.")
  f.no_field.focus()
 }

 if(!number(f.no_field.value))
 {
  validate=false
  alert("Please enter NUMBERS in Number of Field.")
  f.no_field.focus()
 }


 if (validate==true)
 {
  f.submit();
 }
 }
</script>

<BODY>
<form action="field.php" method="post" name="field" onsubmit="validateForm();">

   <p></p>
<?php

echo " Please key in number fields you want";
  ?>       <p></p>
            <tr>
			<td >Number of Fields:</td>
			<td ><input type="text" name="no_field" class="ctr2"></td>

        </tr>


<td align="right"><input type="button" value="confirm" onclick="validateForm();">


 </form>
</BODY>
this is db.php

Code: Select all

<?php

include "../field.php";

$no_field= ($_POST['no_field']);
echo $no_field;
?>
Last edited by lala on Tue Aug 24, 2010 2:23 am, edited 1 time in total.
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Problem in validating number

Post by lala »

May be is javascript's problem? any idea :cry:
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Problem in validating number

Post by PHPHorizons »

Hello lala,

What is "document.db"? I don't see that property created anywhere.
You call "app()" on submit, but I don't see a function named app.
The JavaScript is definitely pretty broken. I would definitely suggest studying up on JavaScript so that you can understand what is happening in this script.
Cheers
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Problem in validating number

Post by lala »

PHPHorizons wrote:Hello lala,

What is "document.db"? I don't see that property created anywhere.
You call "app()" on submit, but I don't see a function named app.
The JavaScript is definitely pretty broken. I would definitely suggest studying up on JavaScript so that you can understand what is happening in this script.
Cheers
Thanks for remind me. I still in the process of learning it.
I had changed the code, and the validate function is work. :)
But how to pass the variable to db.php?
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Problem in validating number

Post by PHPHorizons »

Your form action is field.php. Perhaps you should change that to db.php?

Code: Select all

<form action="field.php" method="post" name="field" onsubmit="validateForm();">

Code: Select all

<form action="db.php" method="post" name="field" onsubmit="validateForm();">
Cheers
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Problem in validating number

Post by lala »

PHPHorizons wrote:Your form action is field.php. Perhaps you should change that to db.php?

Code: Select all

<form action="field.php" method="post" name="field" onsubmit="validateForm();">

Code: Select all

<form action="db.php" method="post" name="field" onsubmit="validateForm();">
Cheers
hi PHPHorizons,
Yes, It does work. Thanks. :D
But somehow I integrate with my original code, it doesnt show the value. I do not know why it happen like that.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Problem in validating number

Post by PHPHorizons »

You're welcome ;)
Post Reply