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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with java field validation

Status
Not open for further replies.

andybid

Vendor
Dec 7, 2010
23
GB
Hi,

I made a form for a client and am having trouble an d can't work out why.

I need to check fields before submitting to save hassle

It is missing the validation routine completely.

The page is at bastions.fast-host.co.cc, have a look at the source and see if you can work it out.

Php happily rejects it if wrong but then they have to go back and start over.

Thanks
 
I am still a bit of a rookie at this but have you tried using the onclick function in the submit input?
 
Hi

andybid said:
The page is at bastions.fast-host.co.cc, have a look at the source and see if you can work it out.
Are you talking about prescription.php ? That has JavaScript validation, but fails because tries to refer the element with [tt]id[/tt] AddVill, but non exists. ( Only AddVill2. )
CompOp said:
have you tried using the onclick function in the submit input?
As forms can be submitted without clicking the submit input, is preferred to set an onsubmit event handler on the form itself.

Feherke.
 
As well as the missing id that [bold]feherke[/b] mentions there are another couple of issues
Code:
	if (name == "") {
        alert( "You didn't enter a name.")[COLOR=red];[/color]
		document.mes.name.focus()[COLOR=red];[/color]
		return false;
Note the missing statement terminators.

I'm sure you have dealt separately with the issues of the Data Protection Act, which will probably impact the site if you are transmitting and/or storing personal data for any reason. Having your site hosted on a free hosting site is unlikely to satisfy the Data Commissioner when the complaints roll in.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
THANKS EVERYONE

I have been over it and found lots of little things like those above.

It now works

The problem I now have is that the name field does not allow spaces, it thinks a space is an illegal character - how do I fix this?

Yes it is prescript.php.

As for DPA, No info is stored in the server. It is emailed straight from the php via smtp to the client - or me at the moment.

I am also the free host. BTW if anyone wants a free hosting account then join up. I will be launching a paid hosting account soon and will give very good prices for members here who want to upgrade to one.

If anyone also wants to help out in that regard on the support line or generally then I will welcome you, and reimburse you a little when the signups start to flow.

Cheers

Andy
 
Hi

Andy said:
The problem I now have is that the name field does not allow spaces, it thinks a space is an illegal character - how do I fix this?
Not so simple. This line says that everything that is not a word character ( letter, digit or underscore ), is illegal :
Code:
[b]var[/b] illegalChars [teal]=[/teal] [fuchsia]/\W/[/fuchsia][teal];[/teal] [gray]// allow letters, numbers, and underscores[/gray]
There is no way to put exception there.

So I would change the expression :
Code:
[b]var[/b] acceptableName [teal]=[/teal] [fuchsia]/^[\w ]{5,25}$/[/fuchsia][teal];[/teal]
Then change the check condition accordingly :
Code:
[b]if[/b] [teal](![/teal]acceptableName[teal].[/teal][COLOR=darkgoldenrod]test[/color][teal]([/teal]name[teal]))[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'Name must have length between 5 and 25[/i][/green][lime][i]\n[/i][/lime][green][i]and contain letters, digits, underscore, space'[/i][/green][teal]);[/teal]
  document[teal].[/teal]mes[teal].[/teal]name[teal].[/teal][COLOR=darkgoldenrod]focus[/color][teal]();[/teal]
  [b]return[/b] [b]false[/b][teal];[/teal]
[teal]}[/teal]
( Note that the quantifier restricts the length, so you can remove the separate checks for empty string and too short or long string. Personally I hate when the input requirements are mentioned one by one, so you have to try and try, until succeed. )

Note that a regular expression for complete name check would be quite difficult. The current one will accept '00000' or ' '. It will also reject 'Mr[red].[/red] Smith', 'Chow Yun[red]-[/red]Fat' or 'El Ni[red]ñ[/red]o' ( the later may depend on the locale settings ).

Feherke.
 
thanks. I'll give that a go when get back home. On mobile phone at present.

How do i get it to reject numbers.

Thanks


 
Hi

Andy said:
How do i get it to reject numbers.
Just change the character class ( the thing between [tt][[/tt] and [tt]][/tt] in the regular expression accordingly :
Code:
[b]var[/b] acceptableName [teal]=[/teal] [fuchsia]/^[\w ]{5,25}$/[/fuchsia][teal];[/teal]
[gray]// PCRE : "\w     any "word" character"[/gray]

[b]var[/b] acceptableName [teal]=[/teal] [fuchsia]/^[A-Za-z ]{5,25}$/[/fuchsia][teal];[/teal]
[gray]// PCRE : "[x-y]       range (can be used for hex characters)"[/gray]

[b]var[/b] acceptableName [teal]=[/teal] [fuchsia]/^[A-Z][A-Za-z ]{4,24}$/[/fuchsia][teal];[/teal]
[gray]// first character must be uppercase letter, following characters can be letters or space[/gray]
( Comments marked with PCRE are quoted from PCRE man page. )

Feherke.
 
thank you for taking the time to explain that. I really must pit down and learn php properly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top