form level validation

Results 1 to 2 of 2

Thread: form level validation

  1. #1
    Join Date
    Dec 1969

    Default form level validation

    i&#039;m validating a field with a included javascript file(thanx to this site) in my script. The attached script works fine when i call it in my page but the only problem is that even if it returns false, it still submits the page. When i cut and paste the code to the &#060;head&#062; tags of the current html page it no longer submits the page when it returns false. i know this its something totally simple, but, as my name would suggest, i wouldn&#039;t know any better.<BR><BR>code: <BR>function ForceNumber(objField, FieldName)<BR>{<BR> var strField = new String(objField.value);<BR> <BR> if (isWhitespace(strField)) return true;<BR><BR> var i = 0;<BR><BR> for (i = 0; i &#060; strField.length; i++)<BR> if (strField.charAt(i) &#060; &#039;0&#039; &#124&#124 strField.charAt(i) &#062; &#039;9&#039;) {<BR> alert(FieldName + " must be a valid numeric entry. Please do not use commas or dollar signs or any non-numeric symbols.");<BR> objField.focus();<BR> return false;<BR> }<BR><BR> return true;<BR>}<BR><BR>how i call it:<BR><BR>ForceNumber(document.form1.txtNumber, "Number") <BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: form level validation

    maybe you should validate the form field(s) in an onsubmit function, called when the user hits the submit button. <BR><BR>function form1_onsubmit()<BR>{<BR>var strField = document.form1.fieldName.value;<BR><BR>var i = 0;<BR><BR>for (i = 0; i &#060; strField.length; i++)<BR>if (strField.charAt(i) &#060; &#039;0&#039; &#124&#124 strField.charAt(i) &#062; &#039;9&#039;) {<BR>alert("FieldName must be a valid numeric entry. Please do not use commas or dollar signs or any non-numeric symbols.");<BR>document.form1.fieldName.focus();<B R>return false;<BR>}<BR><BR>return true;<BR>}<BR><BR><BR>&#060;form name="form1" method="post" action="doit.asp" LANGUAGE="javascript" onsubmit="return form1_onsubmit()"&#062;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts