Jump to content

[SOLVED] Input box and JS


Minase

Recommended Posts

hy there i do have an input box where i want to put a minimum value and a maximum one

like the content should be just numeric so any other keys wont work,and the input to be based on min/max value

example

 

$min = 10

$max = 20

the input box value should be from 10 to 20

user will write it,is not a drop down box

 

thank you

Link to comment
Share on other sites

So... you want to make the input box only accept values between the $min and $max? Easy.

 

<?php

$msg = "";
if ($_POST['submit']) {
    $min = 10;
    $max = 20;
    $value = trim($_POST['value']);
     if (preg_match("/\b\d+\b/", $value)) $msg = "Number is not within range.";
}

echo "<form method='post'><input type='text' name='value' /><br />" . $msg;
?>

 

How's that?

Link to comment
Share on other sites

for javascript try something like

 

<script>
    function checkValue()
    {
         var min = 10;
         var max = 20;
         var val = document.getElementById('inp').value;
         if(val < min || val > max)
         {
               alert('You have failed at inputting');
               document.getElementById('inp').value = 10;
         }
    }
</script>
<input type="text" name="inp" id="inp" onchange="checkValue()" />

Link to comment
Share on other sites

Though, I don't recommend JavaScript because of a few reasons:

1. The alert is very obtrusive when it pops up every time you change the value;

2. The if statement is not guaranteed without checking if the value contains numbers.

 

Edit: Fast edit is annoying for all the special characters it puts into the box.

<?php

$msg = "";
if ($_POST['submit']) {
     $min = 10;
     $max = 20;
     $value = trim($_POST['value']);
     if (preg_match("/\b\d+\b/", $value)) $msg = "Number is not within range.";
}

echo "<form method='post'><input type='text' name='value' /><br />" . $msg;
?>

Link to comment
Share on other sites

ken that requires the page to be reloaded everytime the user enters something. kinda annoying. the best would be to use an ajax script to do it and have a little <div> above the form with all form validation going through that. I just supplied the JavaScript he wanted.

Link to comment
Share on other sites

ken that requires the page to be reloaded everytime the user enters something. kinda annoying. the best would be to use an ajax script to do it and have a little <div> above the form with all form validation going through that. I just supplied the JavaScript he wanted.

Maybe, but it is less annoying than your onchange alert. If a number is 6 figures, I have one reload. Your onchange function can alert up to 6 times, or more if I made a typo. Now that would drive me nuts.

 

Also, why would this require AJAX? ???

Link to comment
Share on other sites

thank you ken ;) but in PHP is easy to do,if i needed to do that in PHP,i would post it into php section.

ofcourse after the JS part,i will use PHP also (JS is just client side,so better secure than vulnerable)

thank you for your code ken

exally from what i can see JS is not so hard,is similar to PHP.

thank you your code is what i exactly needed.

about the alert thing... its not good i will make it if $value < $min { // put the value at minimum, and with maximum also}

so this is better :)

but i will put it to do that just on mouseout,not when users write cause it is anoying ^_^

 

thank you all

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.