SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validating a textfield based on alphabetical letters

    Hi,
    I have a textfield, which takes ring sizes, which happen to be based on a range of letters, say i to v, or is to s. It varies depending on each ring.

    Is there some Javascript function whcih I can use to validate this kind of field?
    thanks a lot

  2. #2
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My immediate reaction was to suggest regular expressions, but it sounds as though the sizes that you're talking about don't fall into any logical range. For instance, you mention one ring having sizes from "is to s". A regular expression wouldn't know the values that came between "is" and "s" and as such you'd have to specify each value individually

    If the range of sizes is different in each case, then wouldn't you be better simply using a select field (as opposed to a text input field) so that the user can only select valid options in the first place?


    Andy
    From the English nation to a US location.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks,
    buyt how would I get the range into the select field?
    In the database I have the min size and max size, what would be used to inout everthing in between into the select field?
    thanks

  4. #4
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you have a separate table which acted like a conversion table?

    e.g. the table lists all letter-sizes in order of size in col1 (SizeAlph) and then associates a numeric value with each in col2 (SizeNum):
    Code:
    (Table)
    C1 C2
    Q   1
    R   2
    S   3
    iS  4
    jS  5
    lS  6
    T   7
    U   8
    Then to find a range between R and js, you simply loop through the sizes' numeric equivalents.

    Getting them into the select is the easy bit.
    You should be able to tweak this so that the results of your query can be assigned to the individual arrays:
    Code:
    <html>
      <head>
    	<title>Dynamic Selects</title>
    	<script language="JavaScript" type="text/javascript">
    	<!--
    	  function writeSelect(ringNumber){
    		Ring1sizes = new Array('i','j','k','l');
    		Ring2sizes = new Array('is','ij','ik','il', 'im');
    		Ring3sizes = new Array('m','n','o');
    		
    		RingSizes = "Ring" + ringNumber + "sizes";
    		
    		document.writeln('Ring ' + ringNumber);
    		document.writeln('<select name="Ring' + ringNumber + '">');
    		for(i=0; i<eval(RingSizes).length;i++){
    		  RingSize = eval(RingSizes)[i];
    		  document.writeln('<option value="' + RingSize + '">' + RingSize + '<\/option>"');
    		}
    		document.writeln('<\/select>');
    	  }
    	//-->
    	</script>
      </head>
      <body>
    	<form>
    	  <script language="JavaScript" type="text/javascript">
    	  <!--
    		writeSelect(1);
    		writeSelect(3);
    		writeSelect(2);
    	  //-->
    	  </script>
    	</form>
      </body>
    </html>
    Andy
    From the English nation to a US location.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI,
    actually, I just thought, cant it be done using regular expressions?
    I will have to get variables dyanmically for each field and insert these in the regular expression

  6. #6
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by damonps2003
    HI,
    actually, I just thought, cant it be done using regular expressions?
    Yes, if you want to make the user guess forever and a day what are valid entries

    Good luck however you decide to tackle it.


    Andy
    From the English nation to a US location.


Bookmarks

Posting Permissions

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