Solved

validation field for ic and telephone

Posted on 2001-08-31
3
996 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
  • 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 30 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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 …
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…

770 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