admin管理员组文章数量:1302970
here is my code and it's not working in my form.
<script type="text/javascript">
function valid(){
var pin_code=document.getElementById("pin");
var user_mobile=document.getElementById("phone");
var user_id=document.getElementById("email");
var pat1=/^([0-9](6,6)+$/;
var pattern=/^([0-9](10,10))+$/;
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9]{3,3})+$/;
if (!filter.test(user_id.vlaue)) {
alert("Email is in www.gmail format");
user_id.focus();
return false;
}
if (!pattern.test(user_mobile.value)) {
alert("Phone nubmer is in 0123456789 format ");
user_mobile.focus();
return false;
}
if (!pat1.test(pin_code.value)) {
alert("Pin code should be 6 digits ");
pin_code.focus();
return false;
}
}
</script>
Here is the issue, when i submit the form whether i enter digits or characters in the mobile number or pin code it's accepting that value also. and when i am using these codes in partitions means like for email
<script type="text/javascript">
function valid() {
var user_id=document.getElementById("email");
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9 {3,3})+$/;
if(!filter.test(user_id.vlaue)) {
alert("Email is in www.gmail format");
user_id.focus();
return false;
}
}
</script>
in this code it's working properly but it's not working when i am using all the codes in one single form.
Please help me. Thank You.
here is my code and it's not working in my form.
<script type="text/javascript">
function valid(){
var pin_code=document.getElementById("pin");
var user_mobile=document.getElementById("phone");
var user_id=document.getElementById("email");
var pat1=/^([0-9](6,6)+$/;
var pattern=/^([0-9](10,10))+$/;
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9]{3,3})+$/;
if (!filter.test(user_id.vlaue)) {
alert("Email is in www.gmail. format");
user_id.focus();
return false;
}
if (!pattern.test(user_mobile.value)) {
alert("Phone nubmer is in 0123456789 format ");
user_mobile.focus();
return false;
}
if (!pat1.test(pin_code.value)) {
alert("Pin code should be 6 digits ");
pin_code.focus();
return false;
}
}
</script>
Here is the issue, when i submit the form whether i enter digits or characters in the mobile number or pin code it's accepting that value also. and when i am using these codes in partitions means like for email
<script type="text/javascript">
function valid() {
var user_id=document.getElementById("email");
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9 {3,3})+$/;
if(!filter.test(user_id.vlaue)) {
alert("Email is in www.gmail. format");
user_id.focus();
return false;
}
}
</script>
in this code it's working properly but it's not working when i am using all the codes in one single form.
Please help me. Thank You.
Share Improve this question edited Feb 25, 2015 at 11:03 M1ke 6,4064 gold badges35 silver badges52 bronze badges asked Feb 25, 2015 at 10:25 Dipika AwasthiDipika Awasthi 111 gold badge1 silver badge4 bronze badges 2- Please give some examples of test data to enter, or even better use jsbin to create an example that we can see and test (just create some HTML elements and paste your JavaScript in) – M1ke Commented Feb 25, 2015 at 10:37
- 1 Your regex is broken. IMO this is the ONLY way to validate an email address: stackoverflow./a/201378/2594742 – AeroX Commented Feb 25, 2015 at 10:51
3 Answers
Reset to default 1Just change pat1
and pattern
to this:
var pat1=/^\d{6}$/;
var pattern=/^\d{10}$/;
Full working java-script as follows:
<script type="text/javascript">
function valid()
{
var pin_code=document.getElementById("pin");
var user_mobile=document.getElementById("phone");
var user_id=document.getElementById("email");
var pat1=/^\d{6}$/;
var pattern=/^\d{10}$/;
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9]{3,3})+$/;
if(!filter.test(user_id.value))
{
alert("Email is in www.gmail. format");
user_id.focus();
return false;
}
if(!pattern.test(user_mobile.value))
{
alert("Phone nubmer is in 0123456789 format ");
user_mobile.focus();
return false;
}
if(!pat1.test(pin_code.value))
{
alert("Pin code should be 6 digits ");
pin_code.focus();
return false;
}
}
</script>
change the pattern for the regular expression as below:
var pat1=/^[0-9]{1,6}$/;
var pattern=/^[0-9]{1,10}$/;
app.directive('validateAddress', function() {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
if (inputValue == null)
return ''
//cleanInputValue = inputValue.replace(/[^\w]/gi, '');
//cleanInputValue = inputValue.replace(/[^a-zA-Z0-9-\,\s\_\.\@\#]/g, "");
cleanInputValue = inputValue.replace(/[^a-zA-Z0-9-\,\s\_\.\@\#\!\$\%\*\(\)\-\+\;\:\>\<\?\|\}\{\=]/g, "");
if (cleanInputValue != inputValue) {
modelCtrl.$setViewValue(cleanInputValue);
modelCtrl.$render();
}
return cleanInputValue;
});
}
}
})
本文标签: regexhow to validate pin codemobile number and email in javascriptStack Overflow
版权声明:本文标题:regex - how to validate pin code, mobile number and email in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741736905a2395098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论