javascriptwcs

User is able to enter blank spaces as input in input paramater in js file


I want to apply validation where user cannot input name starting from blank space, in my present code whe user is giving value using spacebar its successfully entering blank value. I want to apply validation where user can use space but not in the beginning

if($('#workspaceNameUpdate').val().trim().length == 0 && $('#workspaceNameUpdate').val() === "") {  
 $('#workspaceNameUpdate').addClass('error');
 $('#workspaceNameUpdateError').removeClass('hidden');
}

I am using trim class but its not working its not entering into the error block!


Solution

  • I think there should be || instead of &&

    $('#workspaceNameUpdate').val().trim().length == 0
    

    because the first part is trimming and then checking the length - which is right

    but the second part $('#workspaceNameUpdate').val() === "" - is comparing that value and " " != ""

    solution - I think the first check will handle everything and there is no need of second check