1

I need help with PHP or jQuery.

What I need is when user type something in input, if that is valid comes another text. Specifically I need this:

  • if age is less then 18 comes text "You are too young"
  • if age is equal or more then 18 comes text "You are old enough"

This is my PHP code:

$ime = $_POST['ime'];
$godine = $_POST['godine'];
$poruka = $_POST['poruka'];
$nameErr = $old = $young = "";
if (isset($_POST['submit'])) {
    if(empty($_POST['ime'])) {
        $nameErr = "Upisite ime";
    }
    if ($godine < 18) {
        $young = "<span class=\"green\">Premladi ste za nastavak</span>";
    } elseif ($godine >= 18) {
        $old = "<span class=\"blue\">Čestitamo punoljetni ste<span>";
    } 
}

I don't know if that can be done with PHP but i am sure that jQuery did that.

5
  • Do you want it to be done live or after form submission? Commented Apr 2, 2014 at 14:20
  • Hi Kreso, welcome on SO! a few hints to get an answer faster: first of all, try to write all your code in english (except ofcourse for user messages). This way other coders will understand what a variable like $godine or $poruka should hold, without using google translate. Furthermore, try to explain what you have tried to achieve the result you want, and on what specific point you got stuck (is there something you don't understand? are the results different then expected? etc. etc.). Commented Apr 2, 2014 at 14:21
  • 1
    Live, so when user is typing. Commented Apr 2, 2014 at 14:21
  • @giorgio Sorry for that, i am new here :) What i want is when user is typing his age i need basicly live changing text. Etc. input field, i need to write my age, if age is equal or more then 18 i get immediately text by input field, if age is less then 18 i get another text. Commented Apr 2, 2014 at 14:24
  • @KresoGalic No sorry, i'm not mad at you ;) just want to help you! Your comment made it very clear. It would help if you would alter the question and include that comment, and paste a bit of html (the relevant part). although I think your question is already answered by AnoopJoshi below Commented Apr 2, 2014 at 14:26

1 Answer 1

3

Try like this

$("#age").blur(function () {
    if (parseInt($(this).val()) >= 18) {
        $("#message").text("You are old enough");
    } else {
        $("#message").text("You are too young");
    }

});

Html

<span>Age:</span> 
<input type="text" id="age">
<span id="message"></span>

Fiddle

blur() event will fire whenever the focusout happens from the input element. Then you can use .text() method to give the message to user. Here i'm assigning the message to a span.

Sign up to request clarification or add additional context in comments.

2 Comments

i'd change the test to parseInt($(this).val()) >= 18 or parseInt($(this).val()) < 18, as the requirement is 18 years and older (18 inclusive). Just a detail, you got the up though ;)
@giorgio changed the code. Thnks for the suggetion :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.