domino
domino

Reputation: 7345

javascript prompt input

function show_prompt()
{
var name=prompt("Please enter your name");

}

I am calling this function when a button is clicked before submitting the form.

How would I post the 'name' var with form so I can use it with php?

Also, would it be possible to verify the user input before letting it to be submitted? For example to make sure it's at least 4 characters? The user should not be able to click the button before the input meets the criteria. I'm guessing that is not possible, at least with the alert() function. Or could it display another alert message if the input is invalid and re-prompt?

Upvotes: 1

Views: 22409

Answers (1)

TimWolla
TimWolla

Reputation: 32691

function show_prompt() {
    var name;
    do {
        name=prompt("Please enter your name");
    }
    while(name.length < 4);
    $('#myinput').val(name);
}

This will prompt until a name with at least 4 chars is found and will afterwards write it into the Input with the ID myinput.

Demo: http://jsfiddle.net/xy829/ (The input of course can be hidden as well)

Upvotes: 7

Related Questions