0

I am trying to do something different and cleaner but hitting brick-walls. I am new to ASP.NET (None MVC).

I have an HTML paragraph tag which holds an error message, I also added the server control or property so that I can show/hide using Server Side Validation.

I usually wrap the tags in a list so that the form elements are aligned accurately. The default style on the paragraph is set to display: none, this hides the entire paragraph when the page loads, so that there is no DOM delay.

If there is an error when the user hits the submit button, the style is replaced by another style which sets it to: display: block;

When I click the button, the error message displays but quickly goes away like a flickering:

The code looks like this:

<%-- USER NAME ERROR --%>
<li>
<p class="default" runat="server" id="lstUserNameErrorMsg">
<span>Invalid Username:</span>
Numbers, uppercase/lower case letters only. No special characters
</p>

<%-- USERNAME TEXTBOX --%>
<asp:TextBox ID="txtUserName" runat="server" MaxLength="16" TabIndex="4">
</asp:TextBox>
<label runat="server" id="lblUserName" for="txtUserName">* Username</label>
</li>

Here is the JavaScript: (This code is tentative)

<script type="text/javascript">
 function validateForm() {
  var element = document.forms["membership"];
  var userNameFlg = document.getElementById("lstUserNameErrorMsg");

  var isLetters = /^[A-Za-z]+$/;

  if (element.txtUserName.value.match(isLetters) && 
element.txtUserName.value.length >= 2) {

   userNameFlg.setAttribute("class", "default");
  }
  else {
   userNameFlg.setAttribute("class", "failed");
  }
  return false;
 }
</script>


<asp:Button ID="btnSubmit" runat="server" Text="Create Accounte" 
OnClick="btnSubmit_Click" OnClientClick="validateForm()" TabIndex="11" />

This is new territory: Where am I going wrong?

Thanks!

1 Answer 1

2

Add return before the method call to validateForm().

The postback is still happening and hence the paragraph gets hidden again.

<asp:Button ID="btnSubmit" runat="server" Text="Create Accounte" 
OnClick="btnSubmit_Click" OnClientClick="return validateForm()" TabIndex="11" />

Edit:

In your function you are always returning false. So the postback will always fail. Change it like:

function validateForm() {
            var userNameFlg = document.getElementById("lstUserNameErrorMsg");
            var txtBox = document.getElementById("txtUserName");
            var isLetters = /^[A-Za-z0-9]+$/;

            if (txtBox.value != null && txtBox.value.match(isLetters) && txtBox.value.length >= 2) {
                userNameFlg.setAttribute("class", "default");
                return true;
            }
            else {
                userNameFlg.setAttribute("class", "failed");
                return false;
            }
        }

Also since you are just starting out, I suggest you look at jquery. You could have also achieved this through the use of Asp.Net validation controls. Refer MSDN

5
  • Hello @Novice Programmer: Will this stop the form from being submitted? Yes the return did the trick. Thanks for that. Do I still need the return false in the function? Commented Jun 16, 2013 at 19:30
  • yes, return false will ensure the postback stops. Your validation function should perform the validation and return true, if all is ok and false if validation fails. true will allow a postback to happen then Commented Jun 16, 2013 at 19:32
  • Okay I am a little lost: Even after I enter the right values, the form is not posting: I tried adding a Boolean to say if correct return Boolean equals true but that gives me the same problem. When I return false in the function itself after the conditional statement, the function always returns false. I am almost there. So how do I get the function to return true if the right value is entered for the post to occur? Commented Jun 16, 2013 at 19:50
  • sorry just saw your comment, added a response...plus some suggestions. Also changed your regex to allow numeral as it was your requirement Commented Jun 16, 2013 at 20:22
  • Thank you very much. I am tempted to take up JQuery but I want to understand the fundamentals of JavaScript before diving into JQuery. Thanks again for your knowledge. Guy. Commented Jun 16, 2013 at 21:23

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.