?
Solved

Javascript validation submission page with special field

Posted on 2012-03-20
3
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

777 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