Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript validation submission page with special field

Posted on 2012-03-20
3
Medium Priority
?
352 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 1200 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

927 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