Solved

Javascript validation submission page with special field

Posted on 2012-03-20
3
311 Views
Last Modified: 2013-11-19
I need to validate a text field (case_number) in an ASP submission form that contains the following format:

#:##-bk-#####

example: 5:12-bk-12345
or             3:11-bk-99345

Is there anyway that Javascript can validate this specific format? The amount of characters will always be no less than 13, and the : - bk - will always remain a constant.

Any help is greatly appreciated. I attempted the following but it does not work.

Thank you!

if (/^[0-9a-zA-z:-]{13}$/.test(theForm.case_number.value)===false)
{
alert("Case Number format is not correct");
theForm.case_number.focus();
return (false);
}

Open in new window

0
Comment
Question by:arendt73
3 Comments
 
LVL 18

Accepted Solution

by:
nap0leon earned 400 total points
ID: 37745467
I'm sure there's a regex that would work great, but I am incapable of delivering one of them to you.  If no one steps forward with one, you can use this approach in the meantime:

you can split the string into an array, then check the length of the array and the value of each character one at a time.
var str = theForm.case_number.value;
strArray = str.split(''); //this creates an array for every character in the string

Open in new window


or you can examine the characters one at a time or in clumps - regex for checking each of these as being all numbers should be easy to come by
// string.substr(start,length)
var str = '5:12-bk-12345'
var sectionOne = str.substr(0,1) //should be a number
var sectionTwo = str.substr(1,1) //should be a :
var sectionThree = str.substr(2,2) //should be 2 numbers
var sectionFour = str.substr(4,4) //should be -bk-
var sectionFive = str(substr(8,5) //should be 5 numbers

Open in new window


Out of time for the night - hope this helps point you in a workable direction.
0
 
LVL 13

Assisted Solution

by:ansudhindra
ansudhindra earned 100 total points
ID: 37745560
check this

http://jsfiddle.net/Sudhindra/Xmf7f/

var reg = new RegExp("^[0-9][:][0-9]{2}-bk-[0-9]{5}$");
    alert(reg.test(v1));

if it returns true then it is valid, if it is false it is not valid.
0
 

Author Closing Comment

by:arendt73
ID: 37986806
Solution given acceptable.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question