Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello friends: I have hit another brick-wall.

I have the following two functions in an asp.net (not MVC) page: I separated the function because I needed to use each function on an onBlur event, this will simply call individual validation/function. The validation works fine with just the onBlur event but not the combination using the button.

Example: Lets say I enter valid entries into both textboxes and click the submit button, if I go back to the textbox and re-enter a bad value, the onBlur no-longer works.

In a nutshell the onBlur only works if I do not use the button, once I do and the functions have evaluated to true, then the onBlur will not work if I re-enter a wrong value.

Note: I want to learn the basics of JavaScript before jumping into JQuery.

Thanks for your help!

Here is function 1:

function validateA() {
"use strict";

var element = document.forms["membership"];

var userNameErrorMsg = document.getElementById("lstUserNameErrorMsg");
var userNameTextbox = document.getElementById("txtUserName");
var userNameErrorFlg = document.getElementById("lblUserName");


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

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

userNameErrorMsg.setAttribute("class", "default");
userNameTextbox.className = "";
userNameErrorFlg.className = "";

return true;
}
else {

userNameErrorMsg.setAttribute("class", "error-desc");
userNameTextbox.className = "error-state";
userNameErrorFlg.className = "error-flag";

return false;
}
}

And Function 2

function validateB() {
"use strict";

var element = document.forms["membership"];

var passwordErrorDesc = document.getElementById("lstPasswordErrorMsg");
var passwordTextbox = document.getElementById("txtPassword");
var passwordErrorFlg = document.getElementById("lblPassword");


var isNumberLetters = "^[a-zA-Z0-9]*$";

if (!element.txtPassword.value.match(isNumberLetters) && 
element.txtPassword.value.length >= 8) {

passwordErrorDesc.setAttribute("class", "default");
passwordTextbox.className = "";
passwordErrorFlg.className = "";

return true;
}
else {

passwordErrorDesc.setAttribute("class", "error-desc");
passwordTextbox.className = "error-state";
passwordErrorFlg.className = "error-flag";

return false;
}
}

Function to call both functions:

function callBothFunction() {
"use strict";

var isValidFunction = validateA() && validateB();

return isValidFunction;

}

The Button:

<input type="submit" name="btnSubmit" value="Submit Form"
onclick="return callBothFunction();" id="btnSubmit" tabindex="11" />

Finally I am calling the two functions above individually onBlur:

<input name="txtUserName" type="text" maxlength="16" 
id="txtUserName" tabindex="4" onblur="validateA();" />


<input name="txtPassword" type="text" maxlength="16" 
id="txtPassword" tabindex="5" onblur="validateB();" />
share|improve this question
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.