2 Questions about PHP

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

one point I forgot to say is that the textbox only should allow enter positive integers (>0), not negative integers, and not "0" values.

thx for ur help.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

rax369 wrote:one point I forgot to say is that the textbox only should allow enter positive integers (>0), not negative integers, and not "0" values.

thx for ur help.
Natural numbers.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

exactly hob_goblin... u got the point...

thx for the math class :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

so what's wrong with my code snippet (except it doesn't test 0 value) :?:
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

oh volka I'm so sorry w/u :oops: , I hadnt understood how to use ur script, I dont know nothing about java script ok?, so I didnt know how to use it, but after analize that piece of code for a while, something came to my mind watching the order u have used for ur code inside of the standart HTML page(<HTML>, <HEAD>, <BODY>... and so on) ... and I realized how to do that... I tried it and ... WOW 8O !!! IT WORKS :D !!!

THANKS :!: :!: :!:

Could u please give me a favor ?... could u just modify that piece of nice code, to not allow enter '0' please ?

Is that possible for u :?:

thx again man :wink: !
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

here you go

Post by phpScott »

allright rax369

you are right there should be a closing </scipt> tag with the js I guess I just forgot it. Here is the more elegant code with the appropriate script tags.

Code: Select all

<script language="JavaScript" type="text/JavaScript">
function checkInt()
&#123;
 f=document.formName;
   p=f.elements.length;
   for(i=0; i<p; i  )
   &#123;
      if(f.elements&#1111;i].type=="text")
      &#123;
         if(f.elements&#1111;i].value != "")
         &#123;
            if(!isan(f.qty1.value))
            &#123;
                alert("please enter int's only");
               f.qty1.focus()
               break;
            &#125;
         &#125;
      &#125;
   &#125;
&#125;
function isan(string)
&#123;
   if (string.length == 0)
      return false;
   for (var i=0;i < string.length;i  )
   &#123;
      if ((string.substring(i,i 1) < '0') || (string.substring(i,i 1) > '9'))
            return false;
   &#125;
return true;
&#125;
</script>
The only thing you should have to change is the form name, and add two plus signs after the 'i' in the for loop. This doesn't seem to want to show the plus signs.

What this script is doing is in the function checkInt it is getting the form and assigning it to the value 'f'. It saves having to type document.formName over and over.
It then determines how many form elements there are in the page. This includes, your select box, buttons, text fields, essential evertything that has a name/value pair.
It then test to see if those form elements are of type "text"
if it is and the next if statement is true, there is a value in that "text" field then call the next function isan to determine if it is a integer.
This function then does a string search to determine if all the values in that string are numbers. greater or equal to 0 or less then or equal to 9. If it comes back false then there is something else in that string and it will be rejected, and returned back to the first function and the focus will be set on the offending text field.

Hopefully that is enough of an expanation. If it isn't let me know and I will email you a full description of what is going on.

phpScott
:oops: sorry for the dissertation.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

thx to take the time making the script, and explaing it to me phpScott.

excuse my ignorance but now that I got the code, how should I use it ?

The same way I used volka's script :?: ... I just pasted the script part where the js function is been declared inside the <head> </head> part of my php page.
And I called the function from the onkeyup event of my text box.

To show what I did... is what volka showed me to do, this:

Code: Select all

<input type="text" name="textinput" onkeyup="limitToDigits(this);" />
But since I'm watching u r declaring 2 functions in ur js code, how should I use it in my onkeyup event of my text boxes ?

u r declaring 2 functions right :?: how should I call them :?:

in the onkeyup event one followed by the other or :?: :?: :(

thx for any help man ! :wink:
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Yes

Post by phpScott »

Put the entire script inside your <head> tags. It doesn't matter if you have already desclared a <script> tag for other javaScript just make sure that my script is not inside of the other script tags.

The only problem I can see with doing a onKeyUp event is that if some one tries to enter in say 14 mice then it will check every time key pressed. Once for the 1 and agian for the 4.
My suggestion is to alter your <form> tag like so.

<form action="somepage.php" method="post" onSubmit="checkInt()">

then use
<type="submit" name="Submit" value="Check out">

for your submit button in side your form. Change the name and value to your hearts content but it should be a SUBMIT button type.

You don't have to worry about calling the function isan() as it is called from the other function.

I put the two functions seperately because it would be easier to reuse the first function to test for other things on a different page if needed. And besides that is how it was sitting on my computer before I modified it slightly.

hope that helps

phpScott
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

Well I did exactly what u told me phpScott, but it appers doesnt wanna work at all to me.

I dont know if I'm missing something... u tell me.

I added the '++' signs before both 'i' in the for loop, and changed the form name in the js function by the name of my form (prods_req)

I added the part 'onSubmit' to my form tag, as u specified, mine is like this:
<form action="final_check.php" method="post" enctype="multipart/form-data" name="prods_req" id="prods_req" onSubmit="checkInt()">

I already had a submit button, but anyway I checked it, and is like this:
<type="submit" name="Submit" value="Check out">

... so what's the part I'm missing :?: 8O

Shouldnt I specify something inside the parenthesis of the calling of the function checkInt().

I did that when I used volka's function, he told me to put his function like this:

Code: Select all

<input type="text" name="textinput" onkeyup="limitToDigits(this);" />
and as u can see between the parenthesis of the function calling there is the word 'this', I dont know nothing about java script, so I dont know if what I'm saying is totally without sense, it's just a suggestion.

thx for ur help :D
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

sorry

Post by phpScott »

sorry :oops: my mistakes, I didn't look closely enough at the script.
below is a working copy of a html page that is doing what I think you want.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>qty test</title>
</head>
<script language="JavaScript" type="text/JavaScript">
function checkInt()
&#123;
f=document.price;
   p=f.elements.length;
   for(i=0; i<p; i++)
   &#123;
      if(f.elements&#1111;i].type=="text")
      &#123;
         if(f.elements&#1111;i].value != "")
         &#123;
            if(!isan(f.elements&#1111;i].value))
            &#123;
                alert("please enter int's only");
               f.elements&#1111;i].focus()
               break;
            &#125;
         &#125;
      &#125;
   &#125;
&#125;
function isan(string)
&#123;
   if (string.length == 0)
      return false;
   for (var i=0;i < string.length;i++)
   &#123;
      if((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
            return false;
   &#125;
return true;
&#125;
</script>
<body>
 <form action="qty.html" method="post" onSubmit="checkInt()" name="price">
 <input type="text" name="qty" value="">
 <input type="submit" name="Submit" value="Check out">
 <from>
</body>
</html>
just use what you need.

take care
phpScott
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

thx phpScott, I'm gonna try it.

wow... :!: I never though this would become a long thread, of posting solutions for this problem, thx to all of u that help me, guys. :D
Post Reply