469,903 Members | 1,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Form check problem

Hello,

I add a javascript check to each input field. I use an onBlur event. But I
want to add a "batch check" function to a onSubmit event too. In case of
onBlur the call my check like this:
onBlur="isValidInt(this,'Error text',false)

But how can I call in case of onSubmit. There I can't use the "this" and the
following code doesn't work:
isValidInt(document.ageField,'Error text', false);
....

function isValidInt(userInput, errorMsg, isReq){
var mask=/^\d+$/;
if (isReq == false && userInput.value == "") return true;
if (mask.test(userInput.value)) return true;
return false;
}

Thanks!
Jul 23 '05 #1
1 1301
function isValidInt(userInput, errorMsg, isReq){
var mask=/^\d+$/;
if (isReq == false && userInput.value == "") return true;
if (mask.test(userInput.value)) return true;
return false;
}

I suspect that you need to reference the variable userInput.value by its
full name.

Here is an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>form check</title>

<script type="text/javascript">

function validate()
{
alert("in function validate.");
var submitOK = true;

// Notice how I referenced the fields in a form.
// You do not need to make a separate variable.

var theName = document.forms["myForm"].theName.value;
var theEmail = document.forms["myForm"].theEmail.value;

alert(" theName = " + theName +
" theEmail = " + theEmail);

// I changed submitOK to a boolean variable.
var submitOK = true;

// Validate email: name and email

if (theName == '')
{
alert("Please fill in your Name");
submitOK = false;
}
if (theEmail == '')
{
alert("Please fill in Email");
submitOK = false;
}

return submitOK;

}

</script>

</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="http://www.nonamedomain.com"
method="POST"
onsubmit="alert('checing then submitting...');return validate();">
<p>Name:<br>
<input type="text" name="theName" size="20"><br><br>
Email:<br>
<input type="text" name="theEmail" size="20"><br>
</p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
Robert
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Norman Bird | last post: by
1 post views Thread by Sean Abrahams | last post: by
12 posts views Thread by szar | last post: by
4 posts views Thread by Rod Gill | last post: by
27 posts views Thread by Chris | last post: by
1 post views Thread by fugaki | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.