Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Client side form validation 2

Status
Not open for further replies.

nissan240zx

Programmer
Joined
Jun 26, 2005
Messages
280
Location
US
Hello All,
i am working on a ASP form with onSubmit javascript validation.
All the validations works except for few of them.
The ones for checkbox doesnt work, which is "option1" & "agree".
I am also trying to validate a field for Alpha only - which is "assoc_name".

Please help.

Thanks in advance,
Nissan

Code:
	function ValidateForm(){

		if (document.frm.assoc_name.value=="") {
			alert("Please enter Sales Associate Name.")
			document.frm.assoc_name.focus()
		return false }

			
	if (document.frm.option1.value == "") {
			alert("Please check why did customer choose not to follow rules.")
			document.frm.option1.focus()
		return false }
		


		if (document.frm.agree.value=="") {
			alert("You must agree to our terms & conditions, please check the checkbox.")
			document.frm.agree.focus()
		return false }						
		
	}

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
Do it like this.
[tt]
function ValidateForm(){

if (document.frm.assoc_name.value=="") {
alert("Please enter Sales Associate Name.")
document.frm.assoc_name.focus()
return false }


if ([red]![/red]document.frm.option1.[red]checked[/red]) {
alert("Please check why did customer choose not to follow rules.")
document.frm.option1.focus()
return false }



if ([red]![/red]document.frm.agree.[red]checked[/red]) {
alert("You must agree to our terms & conditions, please check the checkbox.")
document.frm.agree.focus()
return false;}

}
[/tt]
If by alpha you mean a-zA-Z, then you can do this for the first part.
[tt]
if (document.frm.assoc_name.value=="") {
alert("Please enter Sales Associate Name.")
document.frm.assoc_name.focus();
return false;
[blue]} else {
if (!/[^a-zA-Z]/.test(document.frm.assoc_name.value)) {
alert("some message on alpha requirement");
return false;
}[/blue]
}
[/tt]
Else, you just add more admissible char to a-zA-Z.
 
Hello tsuji,
This is great...good stuff.

One question though.
What do you mean by "Else, you just add more admissible char to a-zA-Z."
Am I supposed to add anything else to the code to make it work...
Please explain...

Thanks once again..
Nissan...

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
No, Nissan. If you allow only a-zA-Z, then nothing further is needed. I meant for instance you might allow underscore (_) then you add the underscore to [tt]/[^a-zA-Z_]/[/tt]. If further, numeric is allowed, then it would look like [tt]/[^a-zA-Z0-9_]/[/tt]. And in the latter case, there is some special notation for that (\w). But, make it simple and direct at the start. That is, make the script alive not like a stone which is a dead script. And thanks for the vote too.

 
Hello Tsuji,
In ur code didnt get this part
Code:
} else {
            if (!/[^a-zA-Z]/.test(document.frm.assoc_name.value)) {
                alert("some message on alpha requirement");
                return false;
            }
What does "test" reference to test (document.frm.assoc_name.value


Help....
Nissan

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
What does "test" reference to test (document.frm.assoc_name.value
It tests the regular expression that is calling the method. They are very powerful tools, definitely worth learning. In this example:
Code:
/[^a-zA-Z]/
it is looking for a character in a string that is something other than a-z or A-Z (which stand for the lower and upper case alphabet). If such a character is found, the method returns true, false otherwise)

Here are some links on regular expressions that you might wanna check out:
(I have this page printed out and tacked to my office wall)
(this one is really good too)

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Hello Tsuji,
Not sure why..but the validation for only alpha and one of the checkboxes are functioning right..
If you want I would like to post the url of the form so you can take a look at it live and give me some recommendation or alteast know what I am doing.
Let me know and I will post the url.

Nissan..

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
Hello Kaht,
Excellent documents.
Even with value like test its not workng...
any advice...

Nissan

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
Hello Tsuji/Kaht,
the Alpha issue got solved

Code:
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function isAlpha(parm) {return isValid(parm,lwr+upr);}

this works great

the checkbox issue still lingers...:)

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
Hello Tsuji/Kaht,
This is the html/asp code behind the validation for the checkbox....hope that explains..
And I want to make sure that atleast one of the checkboxes are checked.
Code:
<td>
					<input type="checkbox" name="option1" value="ITC not presented as an option">ITC not presented as an option<br>
					<input type="checkbox" name="option1" value="Lease payment too high">Lease payment too high<br>
					<input type="checkbox" name="option1" value="Business accounting reasons">Business accounting reasons<br>
					<input type="checkbox" name="option1" value="Better rate with alternative financing source">Better rate with alternative financing source<br>
					<input type="checkbox" name="option1" value="Existing relationship with alternative financing source">Existing relationship with alternative financing source<br>
					<input type="checkbox" name="option1" value="Issues with ITC in the past">Issues with ITC in the past (describe in comment area)<br>
					<input type="checkbox" name="option1" value="Lease contract too restrictive">Lease contract too restrictive (describe in comment area)<br>
					<input type="checkbox" name="option1" value="Needed special terms not offered by ITC">Needed special terms not offered by ITC (describe in comment area)<br>
					<input type="checkbox" name="option1" value="Program restrictions (Federal business, etc.)">Program restrictions (Federal business, etc.)<br>
					<input type="checkbox" name="option1" value="Credit Declined">Credit Declined<br>
					<input type="checkbox" name="option1" value="Customer had cash available">Customer had cash available<br>
					<input type="checkbox" name="option1" value="ITC took too long to make decision">ITC took too long to make decision<br>
					<input type="checkbox" name="option1" value="Company Policy">Company Policy (describe in comment area)
And the suggested validation was
Code:
  if (!document.frm.option1.checked) {
            alert("Please check why did customer choose not to follow rules.")
            document.frm.option1.focus()
        return false }

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
It is validated like this.
[tt]
var bFound=false;
for (var i=0;i<document.frm.elements["option1"].length;i++) {
if (document.frm.elements["option1"].checked) {
bFound=true;
break;
}
}
if (!bFound) {
alert("Please check why did customer choose not to follow rules.")
document.frm.option1[0].focus();
return false;
}
[/tt]
 
Hello Tsuji,
Before I plug the code..wanted to make sure i am doing it right..
How do i incorporate this piece of code in my existing validation script..

Does it flow like this

Code:
 function ValidateForm(){

        if (document.frm.assoc_name.value=="") {
            alert("Please enter Sales Associate Name.")
            document.frm.assoc_name.focus()
        return false }

var bFound=false;
for (var i=0;i<document.frm.elements["option1"].length;i++) {
    if (document.frm.elements["option1"][i].checked) {
        bFound=true;
        break;
    }
}
if (!bFound) {
    alert("Please check why did customer choose not to follow rules.")
    document.frm.option1[0].focus();
    return false;
}

Does it go as simple as this...
My concern was do I need to place the var statement outside this validation function (function ValidateForm).
Please advice...

Nissan

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
The var bFound should be declared and initialized to false within the function to make it local. I don't see any need to expand its scope.

In passing, to make the notion consistent, I'd forgotten to change the last .focus to [tt]document.frm.elements["option1"][0].focus[/tt] to use the same referencing notion as that used in the if-loop. It shold do as such though. (Just a minor clean up.)
 
Hello Tsuji,
As per your recommendation I will modify the script to

Code:
if (!bFound) {
    alert("Please check why did customer choose not to follow rules.")
    document.frm.elements["option1"][0].focus();
    return false;
}

Thanks once again...

Nissan

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top