0

I have this form where for example in Type A you can write only numbers that start with 1, and so on:

 <form accept-charset="UTF-8" action="verify.php" id="form" method="post">
 <label id="type" for="type" class="">Type</label>
 <select class="" id="Type" name="type">
 <option id="a" value="1">Type A</option>
 <option id="b" value="2">Type B</option>
 <option id="c" value="3">Type C</option>
 <option id="d" value="4">Type D</option>
 <option id="e" value="5">Type E</option>
 </select>
 <label for="type_number" class="inner_text">Type Number</label>
 <input name="type_number" type="text" class="false" id="type_number" />
<input type="button" id="Confirm" value="Confirm" />
 </form>

And this Javascript:

 document.getElementById('Confirm').onclick = function () {

    var letter = document.getElementById("type_number").value.match(document.getElementById("Type").value);
    if (letter !== null) {
        letter = letter[0].toLowerCase();
        this.value = letter + this.value.substring(1);
    }
    else {
    alert('Number is not correct!');
    }
  }

The validation work, but doesn't submit, when a good answer is writed the 'Confirm' get renamed to '1onfirm', only that happen.

Check the jsfiddle too: http://jsfiddle.net/focusoft/hSmFS/1/

Thanks.

1
  • 1
    that's working this.value = letter + this.value.substring(1); sets your confirm button (i.e. this) to 1onfirm Commented Feb 6, 2014 at 17:27

2 Answers 2

2

Either submit the form in javascript function or change the confirm button type to submit.

change your javascript function

document.getElementById('Confirm').onclick = function () {

    var letter = document.getElementById("type_number").value.match(document.getElementById("Type").value);
    if (letter !== null) {
        letter = letter[0].toLowerCase();
        this.value = letter + this.value.substring(1);
        document.forms[0].submit();
    }
    else {
    alert('Number is not correct!');
    }
  }
Sign up to request clarification or add additional context in comments.

3 Comments

If he changed the button to submit he wouldn't need to call submit explicitly.
Thanks @Liam, i just forgot to add 'OR'.
If I change the button to submit the form submit anyway, so this javascript code worked perfectly. Thanks.
1

Change this:

<input type="button" id="Confirm" value="Confirm" />

for this:

<input type="submit" id="Confirm" value="Confirm" />

Comments

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.