Regex with PHP and Javascript - javascript

I cannot understand where i am going wrong.
Simple Form.
<form name="myForm" method="post" action="" onsubmit="return(validate());">
<fieldset>
<label for="phone">Phone Number </label>
<input type="text" name="phone" size="30"><br><div id="badphone" style="background-color: #A9A9F5;"></div>
</fieldset>
</form>
<script>
function validate()
{
var pattern2=new RegExp("[/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/]");
if(document.myForm.phone.value.match(pattern2))
{
var badphone = "<strong>Phone Number can only contain Numbers 0-9</strong>";
document.getElementById("badphone").innerHTML = badphone;
document.myForm.phone.focus() ;
return false;
}
}
</script>
I have tried a couple other Regex's to get this to work but no luck.
I am tring to get an output like:
1112223333, 111-222-3333, (111)222-3333, 111 222 3333, 111.222.3333
Currently it is not validating at all.

Your syntax of using regex using RegExp constructor is wrong.
Your regex can be simplified to:
var pattern2 = /^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]?\d{4}$/;

Why are you using the [/ and /] at the start and ending of the RegEx?
Try removing it.

If want to say not in JS try this
if(!document.myForm.phone.value.match(pattern2))
If the what you have is doing the opposite of what you want change the operator.

Related

The input from client side should entered only digits, if is alphabets should give an error msg

Create an html page with the following form:
<form method="post" name="example" action="">
<p> Enter your name <input type="text"> </p>
<input type="submit" value="Submit Information" />
</form>
<div id="a"></div>
Add a js validation function to the form that ensures that you can only add numbers in the textbox If you enter alphabets, you should generate an error message in the given div. -->
I run the requirement successfully and I'm giving the error message when it entered alphabets. However, it's giving me the same error message when I enter digits as well. Please kindly show how the function or the window.onload should be implemented. Thank you.
My answer is down below;
window.onload = function() {
let form = document.getElementById('form_ref')
form.onsubmit = function() {
let user = form.user.value;
if (parseInt(user) !== user) {
document.querySelector('div').innerHTML = "Error! Please enter digits only!";
return false;
}
return true;
}
}
<form id="form_ref" method="post" name="example" action="">
<label for="username">User</label><input type="text" name="user" id="username" required>
<div id="a"></div>
<br>
<input type="submit" value="Submit Information" id="submit">
</form>
Your equality check parseInt(user) !== user will always return true because form.user.value is a string but parseInt(...) always returns an integer. If you want to check if the entry is an integer there are a couple ways.
You can change the input's type attribute to number to make sure only digits can be entered and then you just have to make sure it's an integer and not a decimal (type="number" still allows decimal numbers so not just digits). user will still be a string, but it's easier to check. I'd recommend using Number.isInteger(...) to do the checking:
if (!Number.isInteger(parseFloat(user))) {
If you really want to use type="text" you can iterate through user and make sure its characters are all digits:
for(let i = 0; i < user.length; i++) {
if("0123456789".indexOf(user[i]) == -1) {
document.querySelector('div').innerHTML = "Error! Please enter digits only!";
return false;
}
}
return true;
One advantage of this method is that you can make more characters available if you want to just by adding them to the string that's searched in the iteration. A disadvantage is that it's slower than the other method (the indexOf method has to iterate through the string for every character of user), but for your use case that seems irrelevant-- this function doesn't need to be called many times per second as it's a simple login type of thing, and it's client-side so you don't need to handle many instances at once. If speed is an issue you could probably make a comparison to the integer equivalencies of the characters:
if(user.charCodeAt(i) < "0".charCodeAt(0) || user.charCodeAt(i) > "9".charCodeAt(0)) {

Limit length of characters in htm in javascript

I have question. I used editable plugin in octobercms project. I can't find this in documentation. How can I limit the length of the characters in my content in html. If there is no any way so how can I do this with JavaScript? I tried to use code like this but I am low in JavaScript.
var x = document.getElementById('gallery');
var tekst= x.outerText;
console.log(tekst);
console.log(x.outerText.length);
if (x.outerText.length > 150) {
var trimmedString = tekst.substring(0, 150);
document.getElementById('gallery').outerText = trimmedString;
}
But now this text not use dic and classes and editable. What can I do to fix this?
Maybe you should do it in the HTML instead.
<form action="demo_form.asp">
Username: <input type="text" name="usrname" maxlength="10"><br>
<input type="submit" value="Submit">
</form>
http://www.w3schools.com/tags/att_input_maxlength.asp
you can just put maxlength="10" for maxlength it will stop the cursor for input after 10 characters.

How to get a value of an input element with id contains special characters?

HTML code:
<input id="lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_:services:dtServicesTable:0:_id68:0:framedRouteIp" name="lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_:services:dtServicesTable:0:_id68:0:framedRouteIp" value="10.9.9.9" class="" type="text">
JavaScript code:
var grade = $('#lfr\\_\\_WSRP\\_e044d147\\_\\_55a7\\_\\_4e6b\\_\\_9e5f\\_\\_938e05d9050c\\_\\:services:dtServicesTable\\:'+i+'\\:\\_id68\\:'+j+'\\:framedRouteIp').val();
but my JavaScript code is not giving me the value.
Should be like the following code since underscore _ is not a special character, you have just one special character : that you have to escape.
Hope this helps.
var i=0,j=0;
var grade = $('#lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_\\:services\\:dtServicesTable\\:'+i+'\\:_id68\\:'+j+'\\:framedRouteIp').val();
$('#result').text(grade);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_:services:dtServicesTable:0:_id68:0:framedRouteIp" name="lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_:services:dtServicesTable:0:_id68:0:framedRouteIp" value="10.9.9.9" class="" type="text">
<br>
<span id='result'></span>
Note : Check comments in question You should seriously reconsider whatever it is you're trying to accomplish with an input ID like that....
You should only escape : as _ is not a special character
So your javascript code will be :
var grade = $('#lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_\\:services\\:dtServicesTable\\:0\\:_id68\\:0\\:framedRouteIp').val();
this will work
use \\ before : only as your Id have : special character _ (underscore is not special character )
Run below code and check output in console.
<script src="//code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var grade = $("#lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_\\:services\\:dtServicesTable\\:0\\:_id68\\:0\\:framedRouteIp").val();
console.log(grade);
});
</script>
<input id="lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_:services:dtServicesTable:0:_id68:0:framedRouteIp" name="lfr__WSRP_e044d147__55a7__4e6b__9e5f__938e05d9050c_:services:dtServicesTable:0:_id68:0:framedRouteIp" value="10.9.9.9" class="" type="text">

Cannot get JavaScript password regex validation to work

I am trying to validate a user entered password. It can only be letters and numbers and I have to use the match method...even if it isn't the best way...I have to use match(). I am missing something to get the working properly. No number or special characters only letters. I do not know much about javascript.
<script type="text/JavaScript">
function chkPwd() {
var pwd = document.form1.pwd;
var pwdMsg = document.getElementById('pwdMsg');
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.match(regex)) {
pwdMsg.innerHTML = "Must contain letters only!"
pwd.select();
return;
}else{
pwdMsg.innerHTML = "";
}
}
</script>
</head>
<body>
<form name="form1" action="" method="post">
<p> Password: <br>
<input type="text" name="pwd" onchange="chkPwd()" />
<span id="pwdMsg"></span></p>
<p>
<input type="button" value="chkPass" onclick="chkPwd()">
</p>
</form>
<div id="results"></div>
</body>
</html>
Your issue appears to be in this section of code.
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.value.match(regx)) {
You are setting the pwd1 to pwd.value, but then on the next line, you are accessing pwd1.value. This means that you are efficiently doing pwd.value.value. Additionally, you are using regx where you should use regex. Also, your if condition does not appear to need a ! in it. I think you mean to do this.
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(pwd1.match(regex)) {
If you want letters only you should use this regex:
^[A-Za-z]+$
If you want to have letters and numbers then use:
^[A-Za-z0-9]+$
Btw, as guys pointed out you are using regx instead of regex

Don't process the <form> if the <input> DOESN'T contain an underscore

Form:
<form action="goto.php" method="post" name="myform" id="myform" onsubmit="return formSubmit(this);" class="form-wrapper cf">
<input name="statusid" autocomplete="off" id="statusid" placeholder="Enter ID Here" type="text">
<input name="submit_btn" class="submit" id="submit_btn" value="" placeholder="submit" >
</form>
Demo - http://jsfiddle.net/FEF7D/4/
Some users submit an ID that has only numbers in it (e.g. 981734844).
And some users submit an ID that has underscore "_" (without quotes) in it (e.g. 28371366_243322).
What I want to do is NOT allowing the users that submit an ID with ONLY numbers in it (e.g. 89172318) to process the form action.
In other words:
82174363278423: Don't allow to process the form action
21489724893249_2918423: Allow to process the form action
Is it possible to do that?
Thanks in advance.
Try this...
function formSubmit(form) {
// create a regex to test for the numbers + underscore + numbers pattern
var rx = /^\d+_\d+$/,
test = rx.test(form.statusid.value);
test || alert('You need to enter an ID with underscore in it.');
return test;
}
Demo - http://jsfiddle.net/FEF7D/7/
you need to use java script validation and in that you can use following code:
if ( String.indexOf('_') != -1 ) {
// your form processing code..
}
Yes you can do that. There are a number of solutions. For example, you can split your input string into an array using the underscore as the split divider and if your array has two entries, you know you've got a single underscore. e.g. Something like this (untested snippet to illustrate - you also need to add numeric checks for each part of the array so you probably want to assign the split to a variable if going with this approach):
$('form').submit(function(event)
{
if (myinputvar.split('_').length ! = 2)
{
event.preventDefault();
return false;
}
// do the rest of your submission here
});
You can do any form validation in onsubmit of form.
HTML
<form name="myform" id="myform" onsubmit="return formSubmit(this);" class="form-wrapper cf" action="goto.php" method="post">
<input name="statusid" autocomplete="off" id="statusid" placeholder="Enter ID Here" type="text">
<input type="submit" name="submit_btn" class="submit" id="submit_btn" value="" placeholder="submit" >
</form>
JavaScript
// Do validation of form
function formSubmit(formObj) {
// Only allowable character is numeric or underscore
if (!/^[_0-9]*$/.test(formObj.statusid.value)) {
return false;
}
return true;
}
The two answers above will work (just strip out Neil's jquery).
Another option is
function formSubmit(form){
var statusid=document.getElementById("statusid");
if(statusid.value.search("_")===-1){
alert('ID must have an underscore "_".');
}
};
unfortunately, for some reason I can't get that to work with jsfiddle, it's telling me formSubmit is not a function, but clearly it is. Either way, javascript has a 'search' method on strings.

Categories

Resources