-1

I have this weird problem with javascript. I wrote a code that checks if the text input from the user is good, and after a while the message saying if it is good or not disappears, any suggestions?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <ul style="text-align:"right" dir="rtl">
    <script type="text/javascript">
        function validate() {
            var ok = true;
            if (FirstName() == false) {
                ok = false;
            }
        }
        function FirstName() {
            document.getElementById("firstName").innerHTML = "";
        var firstName = document.getElementById("firstName").value;
        var length = firstName.length;
        if (length < 2) {
            document.getElementById("firstN").innerHTML = "not good";
            return false;
        }
        else {
            document.getElementById("firstN").innerHTML = "good";
        }
        return true;
        }
    </script>
</head>
<body>

    <form id="form1" runat="server" onsubmit="return validate()" action="Default2.aspx">
    <div>
    enter you first name
    <input type="text" id="firstName"  />
     <label id="firstN"></label>
     <br />
     <input type="submit" value="send"/> 
    </div>
    </form>
</body>
</html>

4
  • 1
    What do you want the code to display exactly? Your question is very unclear Commented Apr 29, 2015 at 9:52
  • I want to check that the user's input have at least 2 letters Commented Apr 29, 2015 at 9:57
  • What document.getElementById("firstName").innerHTML = ""; is supposed to achieve used on an input? Commented Apr 29, 2015 at 10:00
  • it is the only problem is that: after i press submit the output (good or not good) is only for a moment the then disappear and i want that the output will be on the screen and stay there... Commented Apr 29, 2015 at 10:03

2 Answers 2

1

Well,

Please remember as you have mentioned action in the form, the control will transferred to the page mentioned in it when "TRUE" is encountered. If you still want to see "good", you can remove action.

So to get your code working, you can either change it as below,

    <script type="text/javascript">
        function validate() {
            if (FirstName() == false) {
                return false;
            } else {
            	return true;
            }
        }
        function FirstName() {
            document.getElementById("firstName").innerHTML = "";
        var firstName = document.getElementById("firstName").value;
        var length = firstName.length;
        if (length < 2) {
            document.getElementById("firstN").innerHTML = "not good";
            return false;
        }
        else {
            document.getElementById("firstN").innerHTML = "good";
        }
        return true;
        }
    </script>

OR,

You can use jQuery Validation, which is way better and less time consuming,

jQuery Form Validation example

jQuery Validate Options

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

1 Comment

Actually, I removed the action from the code and yet the code seemed to send it to some other link. I guess pre-validation is the answer to this like you mentioned
0

The submit action refreshes the page that's why you only see that for a moment. You have to work with async form if you want to stay on that page.

Read this http://blog.teamtreehouse.com/create-ajax-contact-form

and This https://learn.jquery.com/ajax/ajax-and-forms/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.