0

I am evaluating if the email input is empty or if its not the right pattern. If it is so the emailError is getting value true and further code doesn't execute

if(typeof($rootScope.email) === 'undefined' || /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($scope.email)){
        $scope.emailError = true;
    }

It evaluates the empty input but doesn't check the pattern correctly. Where did I go wrong?

Edit: The html part looks like so:

<input ng-class="{true:'error-bron', false:'nonerror-bron'}[emailError===true]" ng-if="$root.lang ==='en'" type="email" maxlength="40"  required class="bron-input" placeholder="Email" ng-model="$root.email"/>

After that it goes into function if all the fields are valid:

if(!$scope.nameError && !$scope.emailError && !$scope.phoneError){

If I put the pattern validation inside html then it checks it fine on the first time but after the form has been submitted and the user decides to open the pop up again then the values are initially valid and the user can send even an empty email. Hence why Id like to check it inside this function that is executed when user presses submit button.

2 Answers 2

1

Do something like this perhaps:

if(typeof($rootScope.email) === 'undefined' || !(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test($scope.email))){
        $scope.emailError = true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the new pattern but the fault must be elsewhere since in its current form it is not even going through the function on pressing submit
0

Try This :-

<input type="email" name="email" ng-model="email" ng-maxlength="100" ng-model-options="{ updateOn: blur }" ng-pattern = '/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i' required />

2 Comments

Thank you. I did try checking the pattern from html but if I do so it checks the pattern once but if the user opens the pop up with the form again (without refreshing the entire page) then the values are set to valid. At the end of my function that is executed when user presses submit I do set all the values to empty and I even tried adding the touched values. But that didnt help either. $rootScope.email = ""; $rootScope.email.$touched = true;
You are looking for another logic so please provide me with the whole logic to try to help you .. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.