Solved

Javascript validation submission page with special field

Posted on 2012-03-20
3
291 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now