If conditions is not working in javascript injection - javascript

I am working on a project where i want to change the value of the desired text boxes in the web page.
I am using javascript injection to the web browser to paste the values of the text fields.
In the code below, I have taken a activeElement in the document and compare it with other element in the element List. and want to paste another string in the next text field. But in the below code the if----elseif--- condition is not working as desired.
var editcount = document.getElementsByTagName('input');
var fcElement = document.activeElement;
var cpt = 0;
var bFlag = false;
for (cpt = 0; cpt < editcount.length; cpt++) {
if (editcount[cpt].id == fcElement.id && !bFlag) {
fcElement.value = "Username";
bFlag = true;
}
else if((editcount[cpt].type == "password"||editcount[cpt].type == "text" || editcount[cpt].type == "email") && bFlag === true) {
editcount[cpt].value = "Password";
break;
}
}
Here, the password is also copied on the same text field.
can anyone tell me whats wrong with the script ?

Thank you for your help.
I was having problem with my code.
I was comparing the ids of the two elements.
if (editcount[cpt].id == fcElement.id && !bFlag)
and this was not working for some web pages but now I got the solution for it.
I have changed the condition of comparision of elements as below.
if (editcount[cpt].name == fcElement.name && !bFlag)
and My problem has solved.
I am posting my working code here...
`
var editcount = document.getElementsByTagName('input');
var fcElement = document.activeElement;
var cpt = 0;
var bFlag = false;
console.log(fcElement);
for (cpt = 0; cpt < editcount.length; cpt++) {
console.log(editcount[cpt]);
if (editcount[cpt].name == fcElement.name) {
fcElement.value = "Username";
bFlag = true;
} else if ((editcount[cpt].type == "password" || editcount[cpt].type == "text" || editcount[cpt].type == "email") && bFlag === true) {
editcount[cpt].value = "Password";
break;
}
}`
Thank you for your cooperation.
Merry Christmas.

Related

Textarea resize when field is empty

I have problem with this code
I will create a JS addon to resize textarea when textfield is empty,
i think this code is good but not work for me :(
reason = document.getElementById('reason').value !== "";
causes = document.getElementById('causes').value !== "";
corrections = document.getElementById('corrections').value !== "";
comment = document.getElementById('comment').value !== "";
var disabled = $('form-control').is(':disabled') == true;
if (disabled && reason){
$("#reason").attr("rows","5");
}
var disabled = $('form-control').is(':disabled') == true;
if (disabled && causes){
$("#causes").attr("rows","5");
}
var disabled = $('form-control').is(':disabled') == true;
if (disabled && corrections){
$("#reason").attr("rows","5");
}
var disabled = $('form-control').is(':disabled') == true;
if (disabled && comment){
$("#reason").attr("rows","5");
}
You need to not repeat yourself (DRY)
let anyFilled = $("#reason,#causes,#corrections,#comment").filter(function() {
return this.value != "";
}).length>0;
if ($('form-control').is(':disabled') && anyFilled) {
$("#reason").attr("rows", "5");
}
Why are you redeclaring disabled so many times? especially since its the same expression each time? Just declare it once and make sure you brush up on DRY programming
var reason = $('#reason').val() !== "";
var causes = $('#causes').val() !== "";
var corrections = $('#corrections').val() !== "";
var comment = $('#comment').val() !== "";
var disabled = $('form-control').is(':disabled') == true;
if (disabled) {
if (causes || corrections || comment) {
$("#reason").attr("rows", "5");
}
}

Error: '0.type' is null or not an object in javascript

I am getting the error below when I click the button that calls the JavaScript to do the validation. The strange thing is that everything was working before but I am not what happened now. If I select to ignore this error:
Error: '0.type' is null or not an object
then the code works fine but I get the error first then it asks me if i want to debug it, if i select No then the code works fine. Please help. thanks
it seems the code stops at this line:
if (areas[0].type == "textarea") {
but here is my entire code:
<script type ="text/javascript">
function Validate_1() {
var flag = false;
var gridView = document.getElementById('<%= GridView1.ClientID %>');
for (var i = 1; i < gridView.rows.length; i++) {
var selects = gridView.rows[i].getElementsByTagName('select');
//var inputs = gridView.rows[i].getElementsByTagName('input');
var areas = gridView.rows[i].getElementsByTagName('textarea');
if (selects != null && areas != null) {
if (areas[0].type == "textarea") {
var txtval = areas[0].value;
var selectval = selects[0].value;
if (selectval == "No" && (txtval == "" || txtval == null)) {
flag = false;
break;
}
else {
flag = true;
document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'visible';
}
}
}
}
if (!flag) {
alert('Please note that comments are required if you select "No" from the dropdown box. Thanks');
document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'hidden';
// areas[i].focus();
// areas.[i].style.backgroundColor = "red";
}
return flag;
}
// document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'visible';
</script>
var areas = gridView.rows[i].getElementsByTagName('textarea');
getElementsByTagNane does not return null, the length would be zero
So your if check needs to change.
if (selects != null && areas != null)
should be
if (selects.length && areas.length)

form validation with radio buttons and specific errors

I am trying to make a form validate where there are radio buttons and textarea. I want nothing to be left empty i.e the form should be completely filled. I have done the radio buttons part of validation where if a user does not select a radio button he will get an error for that particular question. you can see the code here for detailed code.
Please help me out. I am not getting error for textarea.
Just add another check for textarea
function RadioValidator() {
var ShowAlert = '';
var AllFormElements = window.document.getElementById("FormID").elements;
for (i = 0; i < AllFormElements.length; i++) {
var name = AllFormElements[i].name;
if (AllFormElements[i].type == 'radio') {
....
} else if (AllFormElements[i].type == 'textarea') {
if (AllFormElements[i].value == '') {
ShowAlert += name + ' textarea must be filled\n';
}
}
}
if (ShowAlert !== '') {
alert(ShowAlert);
return false;
} else {
return true;
}
}
you didn't write any validation for 'textarea' block. I have updated it with one textarea... add rest validations.
function RadioValidator()
{
var ShowAlert = '';
var AllFormElements = window.document.getElementById("FormID").elements;
for (i = 0; i < AllFormElements.length; i++)
{
if (AllFormElements[i].type == 'radio')
{
var ThisRadio = AllFormElements[i].name;
var ThisChecked = 'No';
var AllRadioOptions = document.getElementsByName(ThisRadio);
var problem_desc = document.getElementById("problem_desc");
for (x = 0; x < AllRadioOptions.length; x++)
{
if (AllRadioOptions[x].checked && ThisChecked === 'No' && problem_desc.value === "")
{
ThisChecked = 'Yes';
break;
}
}
var AlreadySearched = ShowAlert.indexOf(ThisRadio);
if (ThisChecked == 'No' && AlreadySearched == -1 && problem_desc.value === "")
{
ShowAlert = ShowAlert + ThisRadio + ' option must be selected\n';
}
}else if(AllFormElements[i].type =='textarea')
{
// add your rest of text area validations here
var problem_desc_1 = document.getElementById("problem_desc");
if(problem_desc_1.value === "")
{
ShowAlert = ShowAlert + '"Services (Please Specify)" can not be blank. \n';
}
}
}
if (ShowAlert !== '')
{
alert(ShowAlert);
return false;
}
else
{
return true;
}
}
You need to add a check for textarea as well
In your javascript check you have only added a condition for type radio.
check for textarea type as well and add error if the value is blank.

HTML form validation using JS

I'm creating form validation, but can't figure out how to use checking preg_match from variable. Checking just for empty fields working fine, but its not enough. Any advice?
var tekst = /([a-zA-Z0-9]|[a-zA-Z0-9])/;
var myForm = document.forms.formaa;
var prek = myForm.elements['preke'];
var kaina = myForm.elements['kaina'];
var k = myForm.elements['kiekis'];
var uzsak = myForm.elements['uzsakymas'];
if (uzsak.value == ''){
alert("neuzpildyta");
return false;
}
for (var i = 0; i < prek.length; i++) {
var preke = prek[i];
var kai = kaina[i];
var kie = k[i];
if (preke.value == '' || (preke.value).test(tekst) == false){
alert("neuzpildyta");
return false;
}
if (kai.value == ''){
alert("neuzpildyta");
return false;
}
if (kie.value == ''){
alert("neuzpildyta");
return false;
}
}
You can also use like this,
if (x==null || x=="") {
document.getElementById('name').innerHTML="Name must be filled out";
return false; }
For More This one help's you JavaScript Validation.
Rather than rolling your own validation, have you considered using something like jQuery validation?

How do I create a "check all" link for a web form?

I've got a form with a bunch of checkboxes on it, and I'd like to provide a "check all" link/button.
I'm using the code below, but when it runs, it picks up some radio buttons on the page, and checks/unchecks those too. How do I fix this?
var check = 0;
function doNow()
{
void(d=document);
void(el=d.getElementsByTagName('INPUT'));
for(i=0;i<el.length;i++)
{
if(check == 0)
void(el[i].checked=1)
else
void(el[i].checked=0)
}
if(check == 0)
check = 1;
else
check = 0;
}
You're going to want to check the element's type to ensure you don't accidentally go checking the wrong kind of inputs.
Basic example:
function checkAll( toggle ) {
var inputs = document.getElementsByTagName( 'input' );
for( var i = 0; i < inputs.length; i++ ) {
if( inputs[i].type == 'checkbox' ) {
inputs[i].checked = toggle;
}
}
}
You may want to add other checks, such as only acting on check boxes in a certain logical "group", for example.
You can check the type of the input:
if(el[i].type == 'checkbox') {
el[i].checked = true;
}
You can store the current state as a property of the function as well as check the type
function checkAll() {
var all = document.getElementsByTagName('input');
for( var i = 0, l = all.length; i < l; i++ ) {
if( all[i].type == 'checkbox' ) {
all[i].checked = checkAll.checked;
}
}
checkAll.checked = !checkAll.checked;
}
checkAll.checked = true;

Categories

Resources