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!

Find field names on a page to validate 1

Status
Not open for further replies.

evergrean100

Technical User
Dec 1, 2006
115
US
I am trying to validate all field names on a web page that have _theWord in the fieldname.
Code:
ac_dajhta_theWord
sd_dfdfe_fdfdv
ca_hereeds_theWord
se_dcvdfdef_dsfd
cx_hserj_theWord

In the above I would need to fetch and validate:

ac_dajhta_theWord
ca_hereeds_theWord
cx_hserj_theWord


Please advise because my attempt is not working.
Code:
fuction val()
{
var regexp = /_theWord/;

if(document.myform.name eq regexp)
{
	if(document.myform.name == "")
	{
	   alert("Field value is required")
	 }
}
}

<form name=myform action=goto.cfm onsubmit=return val();>
.....</form>

 
Something like this.

[tt]fuction val()
{
var regexp = /^.*?_theWord$/;
for (var i=0;i<document.myform.elements.length;i++) {
if (regexp.test(document.myform.elements.name) {
if(document.myform.elements.value.replace(/\s/g,"") == "") {
alert("Field value is required")
}
}
}
[/tt]
 
Further notes
After looking at your form line, you should make further two additions.

[1]
><form name=myform action=goto.cfm onsubmit=return val();>
.....</form>

[tt]<form name=[blue]"[/blue]myform[blue]"[/blue] action=[blue]"[/blue]goto.cfm[blue]"[/blue] onsubmit=[red]"[/red]return val();[red]"[/red]>
.....</form>[/tt]

[2] Additional changes to the function val.
[tt]
fuction val()
{
var regexp = /^.*?_theWord$/;
for (var i=0;i<document.myform.elements.length;i++) {
if (regexp.test(document.myform.elements.name) {
if(document.myform.elements.value.replace(/\s/g,"") == "") {
alert("Field value is required");
[blue]return false;[/blue]
}
}
[blue]return true;[/blue]
}
[/tt]
 
Amendment
There is a typo on this line.
[tt] if (regexp.test(document.myform.elements.name)[red])[/red] {[/tt]
 
Thanks!

It works great.

Question about this part:

if(document.myform.elements.value.replace(/\s/g,"") == "")


Why do I need the replace function? Is it getting rid of a space in the string that was created from where??
 
Effectively that's is meaning. But if you are comfortable with a strong form of "exactly" empty, then you sure can spare the replace() part which I thought you might be interested to broadening the scope of validating disallowing pure insignificant whitespace.
[tt]if (document.myform.element.value == "")[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top