1

I have the following code that acts as an interface to the http://www.random.org/integers/ . What I want to do is to validate all the form fields and display alert boxes or such with JavaScript. An example would be setting the numbers to be in a certain range ( in the integer case the min and max) and if they're out of range display a box saying the number has to be between 1 and 1000.

The problem that I am encountering is that the form sends me directly to the random.org website, without taking into consideration my script, it won't even display an alert box it just sends me to their website.

Here's the code.

<form method="get" action="http://www.random.org/integers/">

    <p> Generate 
        <input type="text" name="num" value="100" size="6" maxlength="5" /> 
                    random integers (maximum 10,000).
    </p>

    <p>Each integer should have a value between 
        <input type="text" name="min" value="1" size="12" maxlength="10" /> and 
        <input type="text" name="max" value="100" size="12" maxlength="10" />

    </p>

    <p>Format in 
        <input type="text" name="col" value="5" size="2" maxlength="6" /> columns.
    </p>
    <p> Choose your base: 
         <select name="base">
              <option value="2"> 2 </option>
              <option value="8"> 8 </option>
              <option value="10"> 10 </option>
         </select>

    </p>


    <p>
         <input type="submit" class="submit" value="Get Numbers" onsubmit="analert()" />
         <input type="reset" class="submit" value="Reset Form" />
         <input type="button" class="submit" value="Switch to Advanced Mode"/>
    </p>

    </form>

And the Script:

<script>
function analert()
{
alert("Tralalala");
}
</script>

How can I make this work, display an alert box that says the number is out of range, without being redirected to the random.org website which tells me that the number is out of range?

4
  • This is the html, where is the javascript, and what have you tried to do to solve this problem? Commented Nov 1, 2013 at 11:28
  • you can do two things 1. either check for validation at when numbers are typed in input box and disable submitt button till all validation are ensured or 2. do not use form submission as form action method indeed on submit call a function to check all validation are ensured and if yes manually trigger the form submit event Commented Nov 1, 2013 at 11:29
  • I've added the script, my problem is that it won't take into consideration any of the script, it directly takes the action of the form and redirects me to the website. Indeed I've done something to it so the -1 is unfonded. Commented Nov 1, 2013 at 11:36
  • ya because of the form action its creating the problem wait i update a fiddle to solve it Commented Nov 1, 2013 at 11:40

2 Answers 2

1

you have to check for validation and then decide whether to submit form or not

see here in fiddle

however i have just done for one case you add your further validation try any negative value for generate it will give you error and form will not be submitted

function check(){
if($("#generate").val() < 0 || $("#generate").val() > 10000){
    alert("generate number should be less than 10000");
    return;
}
//add your further condition here

$("#frmsub").click();

}

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

6 Comments

This makes sense. But if I do this and specify an id to each, and then script it like : function check(){ if($("#generate").val() < 0 || $("#generate").val() > 10000){ alert("generate number should be less than 10000"); return; } else if ($("minim").val()< -1000000000|| $("maxim").val() > 1000000000){ alert("The number should be between -1,000,000,000 and 1,000,000,000); return; } //add your further condition here $("#frmsub").click(); } it doesn't display the alert for the min, max thing.
I added the # on the ids but still.
So it won't work with double condition, help me out with some more questions in the future? An email or something.
yes sure, it works in either case or condition see this jsfiddle.net/vinodlouis/5C7yA/4
How can I check if the input field is numeric or not? Like if I enter letters instead of numbers, to pop an alert that it says numbers only or something like that?
|
0

You can start by using some of the input types defined in HTML5:

<input type="number" name="num" maxlength="5" min="20" max="200" step="1" value="100" size="6" /> 

Here we state a min value (I assumed 20 for the purposes of the example) a max value (same again) and a step - which is how much each increment can be.

Modern browsers will validate this, display appropriate messages and highlight fields to be corrected.

You could optionally also add JavaScript validation...

<form id="myForm" method="get" action="http://www.random.org/integers/">

I have added an ID here to avoid infecting the form with JavaScript attributes.

var myForm = document.getElementById('myForm');
myForm.onsubmit = function () {
    // You can code your own validation...
    // return false to prevent submission
    // If everything is okay...
    return true;
};

2 Comments

But won't this validation just redirect me to the website? Like if you add a script to display an alert box, of any kind to the HTML code above, it will ignore it and redirect to the link specified in the action field of the form. How do you get past that and make the code display the alert and not ignore and redirect?
The HTML validation prevents the form from posting - so you have to correct the errors before you could proceed. The same goes for the JavaScript validation - when you return false; from the onsubmit event it will prevent the form submission. Here is an example of both techniques: jsfiddle.net/8X2wy

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.