dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Need to validate text entered starts with the letter S

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    Need to validate text entered starts with the letter S

    Hi, hoping someone can help me!
    I've tried many different fixes for this but evidently I'm not getting it just right! I need javascript to validate that emp_id entered by the user starts with an S or s.

    Company information tells me to do this:

    if (document.User.FirstName.value != "" & document.User.LastName.value != "" & document.User.emp_id.value != "" & (/^S/.test(document.User.emp_id.value)))

    else
    {
    alert("You must enter your name and Student ID. Student ID must begin with an S plus seven digits, before your completion can be recorded.");

    Form entry code is:

    <p align="center">Student ID: <input TYPE="text" id="emp_id" NAME="emp_id" SIZE="8" MAXLENGTH="8">

    I have tried these but they do not work:

    if (document.User.FirstName.value != "" & document.User.LastName.value != "" & document.User.emp_id.value != "" & (/^[S]/.test(document.User.emp_id.value)))

    if (document.User.FirstName.value != "" & document.User.LastName.value != "" & document.User.emp_id.value != "" & (/^S/i.test(document.User.emp_id.value)))

    if (document.User.FirstName.value != "" & document.User.LastName.value != "" & document.User.emp_id.value != "" & (/^[Ss].test(document.User.emp_id.value)))

    Someone told me that the & need to be doubled. But all of the other tests are working. Just not the part about starting with an "s".

    TIA!

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    If you're only checking whether the first letter in emp_id is a S or s, you have plenty of options, although I would just use the String slice function. For example:

    HTML Code:
    <!DOCTYPE html>
    <html>
    	<head>
    		<script type = "text/javascript">						
    			window.onload = function() {
    				if((document.forms[0].ids.value).slice(0,1) == "s" || (document.forms[0].ids.value).slice(0,1) == "S") {
    					alert("begins with a s");
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<form>
    			<input id = "ids" type = "text" value = "something" />
    		</form>
    	</body>
    </html>
    Just replace the elements with the names of the ones you're using.

    As for your if-statements, the logical operator for and technically is &&, however, I have seen & work before as browsers have some lee-way. For example, in Chrome and IE 10, this will execute without errors:

    HTML Code:
    <!DOCTYPE html>
    <html>
    	<head>
    		<script type = "text/javascript">						
    			window.onload = function() {
    				if((document.forms[0].ids.value).slice(0,1) == "s" & document.getElementById("ids").value == "something") {
    					alert("begins with a s");
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<form>
    			<input id = "ids" type = "text" value = "something" />
    		</form>
    	</body>
    </html>

  3. #3
    Join Date
    Mar 2012
    Posts
    1,839
    Just a thought: If the first character always has to be "S", why have it input in the first place? Why not input only the seven digit code and append "S" automatically. You can display an "S" in front of the input box to show that it does not need to be input. E.g.

    ID: S<input id="ids" type="text" maxlength=7>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles