0
function send() {
    alert("Your message sent.");
}

function wrongNickNameorMessage() {
    var nicknameValue = document.getElementById("input-nickname").value;
    var messageValue = document.getElementById("input-text").value;
    if (nicknameValue != "" && messageValue != "") {
        document.getElementById("af-form").submit();
    } else {
        alert("Nickname or message is blank. Please fill.");
        return false;
    }
}

These are my JS codes

<input type="text" name="nickname" id="input-nickname" required>

<textarea name="message" type="text" id="input-text" required></textarea>

<input type="submit" value="Send" onclick="wrongNickNameorMessage() + send()" />

And these are my HTML codes.

When I click on Send button. First alert("Your message sent."); then alert("nickname or message is blank. Please fill."); is working. Or exact opposite.

I wanna disabled send() function if wrongNickNameorMessage() is true.

How can I do that?

3 Answers 3

1

You have the right idea but you're going about it very out-of-the-way. Try this:

function wrongNickNameorMessage() {
    var nicknameValue = document.getElementById("input-nickname").value;
    var messageValue = document.getElementById("input-text").value;
    if (nicknameValue === "" || messageValue === "") {
        alert("Nickname or message is blank or improper input detected. Please fill.");
        return false;
    }
    document.getElementById("af-form").submit();
    alert("Your message sent.");
}

You dont need the other function or the other part of the if statement since you're just validating input. You can get more creative but that's all you really need. Your function will completely stop if there's a problem but otherwise, it'll show the right message and submit.

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

1 Comment

Seems logic. I'm trying, buddy.
1

Although your practice is horrible, this may help you in the future:

/* first give your submit button an id or something and don't use the onclick 
   attribute
*/
<input type='submit' value='Send' id='sub' />

// Now the JavaScript, which should be external for caching.
var doc = document;
// never have to use document.getElementById() again
function E(e){
  return doc.getElementById(e);
}
function send() {
  alert('Your message was sent.');
}
// put all your sub onclick stuff in here
E('sub').onclick = function(){
  var nicknameValue = E('input-nickname').value;
  var messageValue = E('input-text').value;
  if(nicknameValue !== '' && messageValue !== '') {
    send(); E('af-form').submit();
  }
  else {
    alert('Nickname or message is blank. Please fill.');
    return false;
  }
}

Note, that this is not sufficient to handle a form. It just shows concept. JavaScript can be disabled, so you must account for that as well, Server Side.

1 Comment

Yeah I know. I'm a newbie. Thanks buddy.
-1

You need to call a wrapper method that will call the wrongNickNameorMessage() check result and than continue only if returned true.

function conditionalSend(){if (wrongNickNameorMessage()){send();}}

1 Comment

<input type="submit" value="Send" onclick="conditionalSend()" /> I used like this. Your function worked half measure. When I blanked nickname and message your function worked but When I fill them send() function didn't work.

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.