munk.me.uk forum
May 21, 2012, 05:10:49 am *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: SMF - Just Installed!
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Really stupid questions, I'm sure..  (Read 1920 times)
suthen_cowgirl
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 7


View Profile
« on: March 11, 2008, 11:08:48 pm »

I know these will be really stupid questions that I ought to be able to figure out, and I'm already embarassed about asking but...

1.  Since the field validator function is so important to how info is formatted and such, but using it makes a field required, how do I put in optional fields?

2.  I found, on the internet, the proper ... code to convert letters to uppercase or first letter of a string etc.  But my question is for instance on the register page, I have a field for them to insert their state abbreviation and I want to have that field always be capital so where is the best place to add the case senitivity code?

Somewhere in:
Code:
<?php  field_validator("State"$_POST["state"], "alpha"22); ?>

or somewhere in:
Code:
<html><p>State Abbreviation (ex. DC): <input type="text" name="state" value="" maxlength="2"></html>

or Huh?

I'm sure I'll come up with more stupid questions so I titled the subject in a way it can be added to for a long time... Smiley

THANKS as always!
Logged
munk
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 368


View Profile WWW
« Reply #1 on: March 12, 2008, 12:01:49 am »

You could either edit the validator function to include a new 'type' of field to check for, say, called 'us_state' (so you could explicitly check that the two alpha chars entered are in your list of 'valid' states), or more simplistically you could just do what you're doing above, require the user enters exactly 2 alpha chars, and then just use the strtoupper() function to make sure the chars are in upper case - although that doesn't stop someone from entering a string that isn't actually a state!

To do it 'properly', in the 'valid_states' case, first of all reject the input if it's not 2 uppercase alpha chars (or you could just accept 2 alpha chars and do the conversion yourself with strtoupper()).  If that check goes through ok, nextcreate an array of states like:

$valid_states=array(
  
"NY",
  
"MA",
  
"VA",
  
//... etc, no idea if the above are actually US states, sound vaguely like it!
);

then use the in_array() function to test if the input the user submitted is in the $valid_states array.  If the input is in the array, accept it and process it/put it in db or w/e... if not, reject the input and tell the user why it's not valid input ('the input you gave is not a valid state etc'). 

Hope that makes sense!
Logged

~ Jez
suthen_cowgirl
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 7


View Profile
« Reply #2 on: March 12, 2008, 12:15:21 am »

That kinda makes sense..and I might play with it some...but I also want to for instance always make the first letter of their first and last names capital, in addition to doing it to the state field so thats kinda why I asked if it could be done in either of the sections I posted or if it needed to be done somewhere else. 

The more I think about it...I guess it would go something like strtoupper($_POST["state"]...can't really be done as their typing in the form.

And I seem to be really bad about knowing how many instances it needs to change.  I guess anytime something accesses the database..

Sorry...just had to think all this out..

Now for trying to figure out how to create non-required fields...
Logged
munk
Administrator
Sr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 368


View Profile WWW
« Reply #3 on: March 12, 2008, 07:54:21 pm »

There's the ucfirst() function that sets the initial letter of a string to uppercase (ucfirst("foobar") => "Foobar").  Also there's the ucwords() function that does the same but for all words in a string (ucwords("foobar bleh") => "Foobar Bleh").
Logged

~ Jez
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!