Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

validation field for ic and telephone

Posted on 2001-08-31
3
Medium Priority
?
1,104 Views
Last Modified: 2006-11-17
hai, i need javascript solution in validation fields

1.
<INPUT type="text" NAME="ic1" SIZE="6">-<INPUT type="text" NAME="ic2" SIZE="2">-
<INPUT type="text" NAME="ic3" SIZE="4">

the format is like this:
800101-10-5555

ic1 must be 6 digits, ic2 is from 01 to 13(2 digits) , ic3 must also be 4 digits.

2.
also for telephone validation:
<INPUT TYPE="Text" NAME="phone1" SIZE="3">-<INPUT TYPE="Text" NAME="phone2" SIZE="7">
the format is 089-123456 or 010-1234567
0
Comment
Question by:J2J
[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
  • 2
3 Comments
 
LVL 2

Expert Comment

by:psogaa
ID: 6444604
Something like this?( added "maxlength" properties in the input boxes for convenience ).

<html>
<head>
<script language=javascript>
function validateForm( myForm ){
  var ic1Reg = /^\d{6}$/;
  var ic2Reg = /^\d{2}$/;
  var ic3Reg = /^\d{4}$/;
  if( !ic1Reg.test( myForm.ic1.value ) || !ic2Reg.test(myForm.ic2.value) || !ic3Reg.test(myForm.ic3.value)){    
    alert('Incorrect ic format');
    return false;
  }
  else if( myForm.ic2.value > 13 ){
    alert('Ic2 value must be less than 14');
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm(this);">
<INPUT type="text" NAME="ic1" SIZE="6" maxlength=6>-<INPUT type="text" NAME="ic2" SIZE="2" maxlength=2>-
<INPUT type="text" NAME="ic3" SIZE="4" maxlength=4><br><br>
<input type=submit value=submit>
</form>
</body>

0
 
LVL 2

Accepted Solution

by:
psogaa earned 120 total points
ID: 6444623
oops, missed the phone thing.
Here goes :)

<html>
<head>
<script language=javascript>
function validateForm( myForm ){
  var ic1Reg = /^\d{6}$/;
  var ic2Reg = /^\d{2}$/;
  var ic3Reg = /^\d{4}$/;
  var phone1Reg = /^\d{3}$/;
  var phone2Reg = /^\d{6,7}$/;
  if( !ic1Reg.test( myForm.ic1.value ) || !ic2Reg.test(myForm.ic2.value) || !ic3Reg.test(myForm.ic3.value)){    
    alert('Incorrect ic format');
    return false;
  }
  else if( myForm.ic2.value > 13 ){
    alert('Ic2 value must be less than 14');
    return false;
  }
  if( !phone1Reg.test(myForm.phone1.value) || !phone2Reg.test(myForm.phone2.value) ){
    alert('Incorrect phone format');
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm(this);">
<INPUT type="text" NAME="ic1" SIZE="6" maxlength=6>-<INPUT type="text" NAME="ic2" SIZE="2" maxlength=2>-
<INPUT type="text" NAME="ic3" SIZE="4" maxlength=4><br>
<INPUT TYPE="Text" NAME="phone1" SIZE="3" maxlength=3>-<INPUT TYPE="Text" NAME="phone2" SIZE="7" maxlength=7><br><br>
<input type=submit value=submit>
</form>
</body>
0
 

Author Comment

by:J2J
ID: 6470269
thanks for the solution. bye.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

705 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